ROSE  0.9.11.42
BinaryStackDelta.h
1 #ifndef ROSE_BinaryAnalysis_StackDelta_H
2 #define ROSE_BinaryAnalysis_StackDelta_H
3 
4 #include <BaseSemantics2.h>
5 #include <Disassembler.h>
6 #include <Sawyer/Map.h>
7 
8 #include <boost/serialization/access.hpp>
9 
10 namespace Rose {
11 namespace BinaryAnalysis {
12 
13 // Forwards
14 namespace Partitioner2 {
15  class Partitioner;
16  namespace DataFlow {
17  class InterproceduralPredicate;
18  }
19 }
20 
24 namespace StackDelta {
25 
29 // FIXME[Robb Matzke 2015-11-17]: add to Diagnostics.C and dummy functions.
30 void initDiagnostics();
31 
36 
52 class Analysis {
53 public:
56  InstructionSemantics2::BaseSemantics::SValuePtr> SValuePair;
58 
59 private:
61  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer_; // where to start
62 
63  bool hasResults_; // Are the following data members initialized?
64  bool didConverge_; // Are the following data membeers valid (else only approximations)?
65 
66  SValuePair functionStackPtrs_; // Initial and final stack pointers
67  InstructionSemantics2::BaseSemantics::SValuePtr functionDelta_; // Stack delta for entire function
68 
69  SValuePairPerAddress bblockStackPtrs_; // Per-basic block initial and final stack pointers
70  DeltasPerAddress bblockDeltas_; // Stack delta per basic block (net effect of BB on stack ptr)
71 
72  SValuePairPerAddress insnStackPtrs_; // Per-instruction initial and final stack pointers
73  DeltasPerAddress insnDeltas_; // Stack delta per instruction (net effect of insn on stack ptr)
74 
75 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
76 private:
77  friend class boost::serialization::access;
78 
79  template<class S>
80  void serialize(S &s, const unsigned /*version*/) {
81  s & BOOST_SERIALIZATION_NVP(cpu_);
82  s & BOOST_SERIALIZATION_NVP(initialConcreteStackPointer_);
83  s & BOOST_SERIALIZATION_NVP(hasResults_);
84  s & BOOST_SERIALIZATION_NVP(didConverge_);
85  s & BOOST_SERIALIZATION_NVP(functionStackPtrs_);
86  s & BOOST_SERIALIZATION_NVP(functionDelta_);
87  s & BOOST_SERIALIZATION_NVP(bblockStackPtrs_);
88  s & BOOST_SERIALIZATION_NVP(bblockDeltas_);
89  s & BOOST_SERIALIZATION_NVP(insnStackPtrs_);
90  s & BOOST_SERIALIZATION_NVP(insnDeltas_);
91  }
92 #endif
93 
94 public:
101  : hasResults_(false), didConverge_(false) {}
102 
106  explicit Analysis(Disassembler *d)
107  : hasResults_(false), didConverge_(false) {
108  init(d);
109  }
110 
119  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
120 
128  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer() const { return initialConcreteStackPointer_; }
129  void initialConcreteStackPointer(const Sawyer::Optional<rose_addr_t> &val) { initialConcreteStackPointer_ = val; }
137  void
140 
145  bool hasResults() const { return hasResults_; }
146 
151  bool didConverge() const { return didConverge_; }
152 
157  void clearResults();
158 
162  void clearStackPointers();
163 
167  void clearStackDeltas();
168 
173  void clearNonResults();
174 
179  SValuePair functionStackPointers() const { return functionStackPtrs_; }
180 
185  InstructionSemantics2::BaseSemantics::SValuePtr functionStackDelta() const { return functionDelta_; }
186 
191  int64_t functionStackDeltaConcrete() const;
192 
197  SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const;
198 
203  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const;
204 
211  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
212  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
219  int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const;
220 
225  SValuePair instructionStackPointers(SgAsmInstruction*) const;
226 
232  InstructionSemantics2::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction*) const;
233 
240  InstructionSemantics2::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction*) const;
241  InstructionSemantics2::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction*) const;
249 
255  void saveAnalysisResults(SgAsmFunction*) const;
256 
260  static void clearAstStackDeltas(SgNode*);
261 
267 
272  static int64_t toInt(const InstructionSemantics2::BaseSemantics::SValuePtr&);
273 
275  void print(std::ostream&) const;
276 
277 public:
278  // Used internally. Do not document with doxygen.
279  void adjustInstruction(SgAsmInstruction*,
280  const InstructionSemantics2::BaseSemantics::SValuePtr &spIn,
281  const InstructionSemantics2::BaseSemantics::SValuePtr &spOut,
282  const InstructionSemantics2::BaseSemantics::SValuePtr &delta);
283 
284 private:
285  void init(Disassembler*);
286 };
287 
288 std::ostream& operator<<(std::ostream&, const Analysis&);
289 
290 } // namespace
291 } // namespace
292 } // namespace
293 
294 #endif
void initDiagnostics()
Initialize diagnostics.
static int64_t toInt(const InstructionSemantics2::BaseSemantics::SValuePtr &)
Convert a symbolic value to an integer.
InstructionSemantics2::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
InstructionSemantics2::BaseSemantics::SValuePtr functionStackDelta() const
Stack delta for an analyzed function.
void initialConcreteStackPointer(const Sawyer::Optional< rose_addr_t > &val)
Property: Initial value to use for stack pointers.
static void clearAstStackDeltas(SgNode *)
Clear AST stack deltas.
Analysis(const InstructionSemantics2::BaseSemantics::DispatcherPtr &cpu)
Construct an analysis using a specified dispatcher.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
void print(std::ostream &) const
Print multi-line value to specified stream.
int64_t instructionStackDeltaConcrete(SgAsmInstruction *) const
Concrete stack delta for an instruction.
InstructionSemantics2::BaseSemantics::DispatcherPtr cpu() const
Virtual CPU used for analysis.
Represents a synthesized function.
bool didConverge() const
Whether the analysis results are valid.
Analysis(Disassembler *d)
Construct an analyzer using a specified disassembler.
Main namespace for the ROSE library.
InstructionSemantics2::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const
Stack delta for an analyzed basic block.
void clearStackPointers()
Clear stack pointers, not deltas.
void analyzeFunction(const Partitioner2::Partitioner &, const Sawyer::SharedPointer< Partitioner2::Function > &, Partitioner2::DataFlow::InterproceduralPredicate &)
Analyze one function.
int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const
Concrete stack delta for an analyzed basic block.
InstructionSemantics2::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
Reference-counting smart pointer.
Definition: SharedPointer.h:67
InstructionSemantics2::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
Sawyer::Optional< rose_addr_t > initialConcreteStackPointer() const
Property: Initial value to use for stack pointers.
void clearStackDeltas()
Clear stack deltas, not pointers.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
int64_t functionStackDeltaConcrete() const
Concrete stack delta for an analyzed function.
InstructionSemantics2::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction *) const
Stack delta for an instruction.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8908
Predicate that decides when to use inter-procedural data-flow.
Definition: DataFlow.h:116
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
bool hasResults() const
Whether a function has been analyzed.
SValuePair functionStackPointers() const
Initial and final stack pointers for an analyzed function.
void clearResults()
Clear analysis results.
Sawyer::Message::Facility mlog
Facility for diagnostic output.
void saveAnalysisResults(SgAsmFunction *) const
Update AST with analysis results.
SValuePair instructionStackPointers(SgAsmInstruction *) const
Initial and final stack ponters for an analyzed instruction.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:293
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:48
InstructionSemantics2::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const
Initial and final stack pointers for a basic block.
void clearNonResults()
Clears everything but results.