ROSE 0.11.145.192
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#include <boost/serialization/split_member.hpp>
13
14namespace Rose {
15namespace BinaryAnalysis {
16
17// Forwards
18namespace Partitioner2 {
19 namespace DataFlow {
21 }
22}
23
27namespace StackDelta {
28
33
38
42void initNamespace();
43
59class Analysis {
60public:
65
66private:
68 Sawyer::Optional<rose_addr_t> initialConcreteStackPointer_; // where to start
69
70 bool hasResults_; // Are the following data members initialized?
71 bool didConverge_; // Are the following data membeers valid (else only approximations)?
72
73 SValuePair functionStackPtrs_; // Initial and final stack pointers
74 InstructionSemantics::BaseSemantics::SValuePtr functionDelta_; // Stack delta for entire function
75
76 SValuePairPerAddress bblockStackPtrs_; // Per-basic block initial and final stack pointers
77 DeltasPerAddress bblockDeltas_; // Stack delta per basic block (net effect of BB on stack ptr)
78
79 SValuePairPerAddress insnStackPtrs_; // Per-instruction initial and final stack pointers
80 SValuePairPerAddress insnFramePtrs_; // Per-instruction initial and final frame pointers if known
81 DeltasPerAddress insnSpDeltas_; // Stack delta per instruction (net effect of insn on stack ptr)
82 bool hasConsistentFramePointer_ = false; // Arch has FP register that's used as a frame pointer for this function
83
84#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
85private:
86 friend class boost::serialization::access;
87
88 template<class S>
89 void serializeCommon(S &s, const unsigned version) {
90 if (version < 1)
91 s & BOOST_SERIALIZATION_NVP(cpu_);
92 s & BOOST_SERIALIZATION_NVP(initialConcreteStackPointer_);
93 s & BOOST_SERIALIZATION_NVP(hasResults_);
94 s & BOOST_SERIALIZATION_NVP(didConverge_);
95 s & BOOST_SERIALIZATION_NVP(functionStackPtrs_);
96 s & BOOST_SERIALIZATION_NVP(functionDelta_);
97 s & BOOST_SERIALIZATION_NVP(bblockStackPtrs_);
98 s & BOOST_SERIALIZATION_NVP(bblockDeltas_);
99 s & BOOST_SERIALIZATION_NVP(insnStackPtrs_);
100 s & BOOST_SERIALIZATION_NVP(insnSpDeltas_);
101 if (version >= 2) {
102 s & BOOST_SERIALIZATION_NVP(insnFramePtrs_);
103 s & BOOST_SERIALIZATION_NVP(hasConsistentFramePointer_);
104 }
105 }
106
107 template<class S>
108 void save(S &s, const unsigned version) const {
109 const_cast<Analysis*>(this)->serializeCommon(s, version);
110 if (version >= 1) {
111 std::string archName;
113 if (cpu_) {
114 archName = Architecture::name(cpu_->architecture());
115 ops = cpu_->operators();
116 }
117 s & BOOST_SERIALIZATION_NVP(archName);
118 s & BOOST_SERIALIZATION_NVP(ops);
119 }
120 }
121
122 template<class S>
123 void load(S &s, const unsigned version) {
124 serializeCommon(s, version);
125 if (version >= 1) {
126 std::string archName;
128 s & BOOST_SERIALIZATION_NVP(archName);
129 s & BOOST_SERIALIZATION_NVP(ops);
130 if (!archName.empty())
131 cpu_ = Architecture::newInstructionDispatcher(archName, ops);
132 }
133 }
134
135 BOOST_SERIALIZATION_SPLIT_MEMBER();
136#endif
137
138public:
145 : hasResults_(false), didConverge_(false) {}
146
151 : hasResults_(false), didConverge_(false) {
152 init(d);
153 }
154
163 : cpu_(cpu), hasResults_(false), didConverge_(false) {}
164
172 Sawyer::Optional<rose_addr_t> initialConcreteStackPointer() const { return initialConcreteStackPointer_; }
173 void initialConcreteStackPointer(const Sawyer::Optional<rose_addr_t> &val) { initialConcreteStackPointer_ = val; }
181 void
184
189 bool hasResults() const { return hasResults_; }
190
195 bool didConverge() const { return didConverge_; }
196
202
207
210
215
221
226 SValuePair functionStackPointers() const { return functionStackPtrs_; }
227
233
239
244 SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const;
245
251
266 int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const;
267
273
280
306
313
318
324
330
335
337 void print(std::ostream&) const;
338
339public:
340 // Used internally. Do not document with doxygen.
341 void adjustInstruction(SgAsmInstruction*,
342 const InstructionSemantics::BaseSemantics::SValuePtr &spIn, // SP before execution
343 const InstructionSemantics::BaseSemantics::SValuePtr &spOut, // SP after execution
344 const InstructionSemantics::BaseSemantics::SValuePtr &spDelta, // spOut - spIn
345 const InstructionSemantics::BaseSemantics::SValuePtr &fpIn, // FP before execution
346 const InstructionSemantics::BaseSemantics::SValuePtr &fpOut); // FP after execution
347
348private:
349 void init(const Disassembler::BasePtr&);
350};
351
352std::ostream& operator<<(std::ostream&, const Analysis&);
353
354} // namespace
355} // namespace
356} // namespace
357
358// Class versions must be at global scope
359BOOST_CLASS_VERSION(Rose::BinaryAnalysis::StackDelta::Analysis, 2);
360
361#endif
362#endif
Various tools for data-flow analysis.
Definition DataFlow.h:72
Predicate that decides when to use inter-procedural data-flow.
InstructionSemantics::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
SValuePair instructionStackPointers(SgAsmInstruction *) const
Initial and final stack ponters for an analyzed instruction.
void analyzeFunction(const Partitioner2::PartitionerConstPtr &, const Partitioner2::FunctionPtr &, Partitioner2::DataFlow::InterproceduralPredicate &)
Analyze one function.
static Sawyer::Optional< int64_t > toInt(const InstructionSemantics::BaseSemantics::SValuePtr &)
Convert a symbolic value to an integer.
void clearStackDeltas()
Clear stack deltas, not pointers.
InstructionSemantics::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction *) const
Stack delta for an instruction.
void clearFramePointers()
Clear the frame pointers.
Sawyer::Optional< rose_addr_t > initialConcreteStackPointer() const
Property: Initial value to use for stack pointers.
Definition StackDelta.h:172
SValuePair functionStackPointers() const
Initial and final stack pointers for an analyzed function.
Definition StackDelta.h:226
int64_t instructionStackDeltaConcrete(SgAsmInstruction *) const
Concrete stack delta for an instruction.
void print(std::ostream &) const
Print multi-line value to specified stream.
void initialConcreteStackPointer(const Sawyer::Optional< rose_addr_t > &val)
Property: Initial value to use for stack pointers.
Definition StackDelta.h:173
void saveAnalysisResults(SgAsmFunction *) const
Update AST with analysis results.
static void clearAstStackDeltas(SgNode *)
Clear AST stack deltas.
InstructionSemantics::BaseSemantics::SValuePtr functionStackDelta() const
Stack delta for an analyzed function.
Definition StackDelta.h:232
InstructionSemantics::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
int64_t functionStackDeltaConcrete() const
Concrete stack delta for an analyzed function.
InstructionSemantics::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const
Stack delta for an analyzed basic block.
InstructionSemantics::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
void clearNonResults()
Clears everything but results.
bool hasConsistentFramePointer() const
True if the function appears to have a frame pointer.
InstructionSemantics::BaseSemantics::SValuePtr instructionInputFrameDelta(SgAsmInstruction *) const
Frame delta for an instruction.
Analysis(const InstructionSemantics::BaseSemantics::DispatcherPtr &cpu)
Construct an analysis using a specified dispatcher.
Definition StackDelta.h:162
int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const
Concrete stack delta for an analyzed basic block.
void clearStackPointers()
Clear stack pointers, not deltas.
InstructionSemantics::BaseSemantics::DispatcherPtr cpu() const
Virtual CPU used for analysis.
Definition StackDelta.h:323
bool hasResults() const
Whether a function has been analyzed.
Definition StackDelta.h:189
SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const
Initial and final stack pointers for a basic block.
InstructionSemantics::BaseSemantics::SValuePtr instructionOutputFrameDelta(SgAsmInstruction *) const
Frame delta for an instruction.
void clearResults()
Clear analysis results.
Analysis(const Disassembler::BasePtr &d)
Construct an analyzer using a specified disassembler.
Definition StackDelta.h:150
InstructionSemantics::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
bool didConverge() const
Whether the analysis results are valid.
Definition StackDelta.h:195
Container associating values with keys.
Definition Sawyer/Map.h:72
Collection of streams.
Definition Message.h:1606
Holds a value or nothing.
Definition Optional.h:56
Represents a synthesized function.
Base class for machine instructions.
This class represents the base class for all IR nodes within Sage III.
InstructionSemantics::BaseSemantics::DispatcherPtr newInstructionDispatcher(const std::string &name, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &)
Create a new instruction dispatcher by name.
const std::string & name(const BaseConstPtr &)
Architecture name free function.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
Sawyer::Message::Facility mlog
Facility for diagnostic output.
void initDiagnostics()
Initialize diagnostics.
void initNamespace()
Initialize this namespace.
The ROSE library.