ROSE  0.9.9.109
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 
22 namespace StackDelta {
23 
27 // FIXME[Robb Matzke 2015-11-17]: add to Diagnostics.C and dummy functions.
28 void initDiagnostics();
29 
34 
38 class Analysis {
39 public:
42  InstructionSemantics2::BaseSemantics::SValuePtr> SValuePair;
44 
45 private:
47  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer_; // where to start
48 
49  bool hasResults_; // Are the following data members initialized?
50  bool didConverge_; // Are the following data membeers valid (else only approximations)?
51 
52  SValuePair functionStackPtrs_; // Initial and final stack pointers
53  InstructionSemantics2::BaseSemantics::SValuePtr functionDelta_; // Stack delta for entire function
54 
55  SValuePairPerAddress bblockStackPtrs_; // Per-basic block initial and final stack pointers
56  DeltasPerAddress bblockDeltas_; // Stack delta per basic block (net effect of BB on stack ptr)
57 
58  SValuePairPerAddress insnStackPtrs_; // Per-instruction initial and final stack pointers
59  DeltasPerAddress insnDeltas_; // Stack delta per instruction (net effect of insn on stack ptr)
60 
61 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
62 private:
63  friend class boost::serialization::access;
64 
65  template<class S>
66  void serialize(S &s, const unsigned version) {
67  s & BOOST_SERIALIZATION_NVP(cpu_);
68  s & BOOST_SERIALIZATION_NVP(initialConcreteStackPointer_);
69  s & BOOST_SERIALIZATION_NVP(hasResults_);
70  s & BOOST_SERIALIZATION_NVP(didConverge_);
71  s & BOOST_SERIALIZATION_NVP(functionStackPtrs_);
72  s & BOOST_SERIALIZATION_NVP(functionDelta_);
73  s & BOOST_SERIALIZATION_NVP(bblockStackPtrs_);
74  s & BOOST_SERIALIZATION_NVP(bblockDeltas_);
75  s & BOOST_SERIALIZATION_NVP(insnStackPtrs_);
76  s & BOOST_SERIALIZATION_NVP(insnDeltas_);
77  }
78 #endif
79 
80 public:
87  : hasResults_(false), didConverge_(false) {}
88 
92  explicit Analysis(Disassembler *d)
93  : hasResults_(false), didConverge_(false) {
94  init(d);
95  }
96 
105  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
106 
114  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer() const { return initialConcreteStackPointer_; }
115  void initialConcreteStackPointer(const Sawyer::Optional<rose_addr_t> &val) { initialConcreteStackPointer_ = val; }
123  void
126 
131  bool hasResults() const { return hasResults_; }
132 
137  bool didConverge() const { return didConverge_; }
138 
143  void clearResults();
144 
148  void clearStackPointers();
149 
153  void clearStackDeltas();
154 
159  void clearNonResults();
160 
165  SValuePair functionStackPointers() const { return functionStackPtrs_; }
166 
171  InstructionSemantics2::BaseSemantics::SValuePtr functionStackDelta() const { return functionDelta_; }
172 
177  int64_t functionStackDeltaConcrete() const;
178 
183  SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const;
184 
189  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const;
190 
197  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
198  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
205  int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const;
206 
211  SValuePair instructionStackPointers(SgAsmInstruction*) const;
212 
218  InstructionSemantics2::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction*) const;
219 
226  InstructionSemantics2::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction*) const;
227  InstructionSemantics2::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction*) const;
235 
241  void saveAnalysisResults(SgAsmFunction*) const;
242 
246  static void clearAstStackDeltas(SgNode*);
247 
253 
258  static int64_t toInt(const InstructionSemantics2::BaseSemantics::SValuePtr&);
259 
261  void print(std::ostream&) const;
262 
263 public:
264  // Used internally. Do not document with doxygen.
265  void adjustInstruction(SgAsmInstruction*,
266  const InstructionSemantics2::BaseSemantics::SValuePtr &spIn,
267  const InstructionSemantics2::BaseSemantics::SValuePtr &spOut,
268  const InstructionSemantics2::BaseSemantics::SValuePtr &delta);
269 
270 private:
271  void init(Disassembler*);
272 };
273 
274 std::ostream& operator<<(std::ostream&, const Analysis&);
275 
276 } // namespace
277 } // namespace
278 } // namespace
279 
280 #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:34
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:8322
Predicate that decides when to use inter-procedural data-flow.
Definition: DataFlow.h:97
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:289
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:41
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.