ROSE  0.10.7.0
BinaryStackDelta.h
1 #ifndef ROSE_BinaryAnalysis_StackDelta_H
2 #define ROSE_BinaryAnalysis_StackDelta_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <BaseSemantics2.h>
7 #include <Disassembler.h>
8 #include <Sawyer/Map.h>
9 
10 #include <boost/serialization/access.hpp>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 
15 // Forwards
16 namespace Partitioner2 {
17  class Partitioner;
18  namespace DataFlow {
19  class InterproceduralPredicate;
20  }
21 }
22 
26 namespace StackDelta {
27 
31 // FIXME[Robb Matzke 2015-11-17]: add to Diagnostics.C and dummy functions.
32 void initDiagnostics();
33 
38 
54 class Analysis {
55 public:
58  InstructionSemantics2::BaseSemantics::SValuePtr> SValuePair;
60 
61 private:
63  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer_; // where to start
64 
65  bool hasResults_; // Are the following data members initialized?
66  bool didConverge_; // Are the following data membeers valid (else only approximations)?
67 
68  SValuePair functionStackPtrs_; // Initial and final stack pointers
69  InstructionSemantics2::BaseSemantics::SValuePtr functionDelta_; // Stack delta for entire function
70 
71  SValuePairPerAddress bblockStackPtrs_; // Per-basic block initial and final stack pointers
72  DeltasPerAddress bblockDeltas_; // Stack delta per basic block (net effect of BB on stack ptr)
73 
74  SValuePairPerAddress insnStackPtrs_; // Per-instruction initial and final stack pointers
75  DeltasPerAddress insnDeltas_; // Stack delta per instruction (net effect of insn on stack ptr)
76 
77 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
78 private:
79  friend class boost::serialization::access;
80 
81  template<class S>
82  void serialize(S &s, const unsigned /*version*/) {
83  s & BOOST_SERIALIZATION_NVP(cpu_);
84  s & BOOST_SERIALIZATION_NVP(initialConcreteStackPointer_);
85  s & BOOST_SERIALIZATION_NVP(hasResults_);
86  s & BOOST_SERIALIZATION_NVP(didConverge_);
87  s & BOOST_SERIALIZATION_NVP(functionStackPtrs_);
88  s & BOOST_SERIALIZATION_NVP(functionDelta_);
89  s & BOOST_SERIALIZATION_NVP(bblockStackPtrs_);
90  s & BOOST_SERIALIZATION_NVP(bblockDeltas_);
91  s & BOOST_SERIALIZATION_NVP(insnStackPtrs_);
92  s & BOOST_SERIALIZATION_NVP(insnDeltas_);
93  }
94 #endif
95 
96 public:
103  : hasResults_(false), didConverge_(false) {}
104 
108  explicit Analysis(Disassembler *d)
109  : hasResults_(false), didConverge_(false) {
110  init(d);
111  }
112 
121  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
122 
130  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer() const { return initialConcreteStackPointer_; }
131  void initialConcreteStackPointer(const Sawyer::Optional<rose_addr_t> &val) { initialConcreteStackPointer_ = val; }
139  void
142 
147  bool hasResults() const { return hasResults_; }
148 
153  bool didConverge() const { return didConverge_; }
154 
159  void clearResults();
160 
164  void clearStackPointers();
165 
169  void clearStackDeltas();
170 
175  void clearNonResults();
176 
181  SValuePair functionStackPointers() const { return functionStackPtrs_; }
182 
187  InstructionSemantics2::BaseSemantics::SValuePtr functionStackDelta() const { return functionDelta_; }
188 
193  int64_t functionStackDeltaConcrete() const;
194 
199  SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const;
200 
205  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const;
206 
213  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
214  InstructionSemantics2::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
221  int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const;
222 
227  SValuePair instructionStackPointers(SgAsmInstruction*) const;
228 
234  InstructionSemantics2::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction*) const;
235 
242  InstructionSemantics2::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction*) const;
243  InstructionSemantics2::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction*) const;
251 
257  void saveAnalysisResults(SgAsmFunction*) const;
258 
262  static void clearAstStackDeltas(SgNode*);
263 
269 
274  static int64_t toInt(const InstructionSemantics2::BaseSemantics::SValuePtr&);
275 
277  void print(std::ostream&) const;
278 
279 public:
280  // Used internally. Do not document with doxygen.
281  void adjustInstruction(SgAsmInstruction*,
282  const InstructionSemantics2::BaseSemantics::SValuePtr &spIn,
283  const InstructionSemantics2::BaseSemantics::SValuePtr &spOut,
284  const InstructionSemantics2::BaseSemantics::SValuePtr &delta);
285 
286 private:
287  void init(Disassembler*);
288 };
289 
290 std::ostream& operator<<(std::ostream&, const Analysis&);
291 
292 } // namespace
293 } // namespace
294 } // namespace
295 
296 #endif
297 #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:1600
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 intrusive smart pointer.
Definition: SharedPointer.h:68
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< 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:9247
Predicate that decides when to use inter-procedural data-flow.
Definition: DataFlow.h:119
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.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
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:321
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50
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.