ROSE  0.11.50.0
PointerDetection.h
1 #ifndef ROSE_BinaryAnalysis_PointerDetection_H
2 #define ROSE_BinaryAnalysis_PointerDetection_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics.h>
7 #include <Sawyer/Set.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 
12 // Forwards
13 class Disassembler;
14 namespace Partitioner2 {
15  class Partitioner;
16  class Function;
17 }
18 
19 
112 namespace PointerDetection {
113 
117 void initDiagnostics();
118 
123 
125 struct Settings {
132 
138 
141  : ignoreConstIp(true), ignoreStrangeSizes(true) {}
142 };
143 
147  size_t nBits;
149  PointerDescriptor(const SymbolicExpr::Ptr &lvalue, size_t nBits)
150  : lvalue(lvalue), nBits(nBits) {}
151 };
152 
155 public:
156  bool operator()(const PointerDescriptor &a, const PointerDescriptor &b) const;
157 };
158 
160 typedef std::set<PointerDescriptor, PointerDescriptorLessp> PointerDescriptors;
161 
166 class Analysis {
167 public:
168 
169 private:
170  Settings settings_;
172  bool hasResults_; // Are the following data members initialized?
173  bool didConverge_; // Are the following data members valid (else only appoximations)?
174  PointerDescriptors codePointers_; // Memory addresses that hold a pointer to code
175  PointerDescriptors dataPointers_; // Memory addresses that hold a pointer to data
176  InstructionSemantics2::BaseSemantics::StatePtr initialState_; // Initial state for analysis
177  InstructionSemantics2::BaseSemantics::StatePtr finalState_; // Final state for analysis
178 
179 public:
186  : hasResults_(false), didConverge_(false) {}
187 
191  explicit Analysis(Disassembler *d, const Settings &settings = Settings())
192  : hasResults_(false), didConverge_(false) {
193  init(d);
194  }
195 
203  const Settings &settings = Settings())
204  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
205 
209  const Settings& settings() const { return settings_; }
210 
216  void analyzeFunction(const Partitioner2::Partitioner&, const Sawyer::SharedPointer<Partitioner2::Function>&);
217 
222  bool hasResults() const { return hasResults_; }
223 
228  bool didConverge() const { return didConverge_; }
229 
234  void clearResults();
235 
240  void clearNonResults();
241 
246  const PointerDescriptors& codePointers() const {
247  return codePointers_;
248  }
249 
254  const PointerDescriptors& dataPointers() const {
255  return dataPointers_;
256  }
257 
264  return initialState_;
265  }
266 
273  return finalState_;
274  }
275 
276 private:
277  void init(Disassembler*);
278 
280  makeRiscOperators(const Partitioner2::Partitioner&) const;
281 
282  // Prints instructions to the mlog[DEBUG] diagnostic stream if that stream is enabled.
283  void
284  printInstructionsForDebugging(const Partitioner2::Partitioner&, const Sawyer::SharedPointer<Partitioner2::Function>&);
285 
286  // Given a potential pointer's r-value, determine if the r-value is a pointer and if so, store its address in the
287  // result. The pointer's value and the defining instructions are added to the two sets, and the result is not updated for
288  // values and instructions that have already been processed.
289  void
290  conditionallySavePointer(const InstructionSemantics2::BaseSemantics::SValuePtr &ptrValue,
291  Sawyer::Container::Set<uint64_t> &ptrValueSeen,
292  size_t wordSize, PointerDescriptors &result);
293 };
294 
295 } // namespace
296 } // namespace
297 } // namespace
298 
299 #endif
300 #endif
Ordered set of values.
Definition: Set.h:52
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Settings to control the pointer analysis.
InstructionSemantics2::BaseSemantics::StatePtr finalState() const
Final state for analysis.
Collection of streams.
Definition: Message.h:1606
bool didConverge() const
Whether the analysis results are valid.
Analysis(Disassembler *d, const Settings &settings=Settings())
Construct an analysis using a specific disassembler.
InstructionSemantics2::BaseSemantics::StatePtr initialState() const
Initial state for analysis.
const Settings & settings() const
Property: Analysis settings.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
bool ignoreConstIp
Whether to ignore branches to concrete addresses.
void clearNonResults()
Clears everything but results.
Main namespace for the ROSE library.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Analysis(const InstructionSemantics2::BaseSemantics::DispatcherPtr &cpu, const Settings &settings=Settings())
Construct an analysis using a specified dispatcher.
Sawyer::Message::Facility mlog
Facility for diagnostic output.
void initDiagnostics()
Initialize diagnostics.
Functor to compare two PointerLocation objects.
void clearResults()
Clear analysis results.
SymbolicExpr::Ptr lvalue
Symbolic address of pointer.
const PointerDescriptors & codePointers() const
Property: Code pointers.
std::set< PointerDescriptor, PointerDescriptorLessp > PointerDescriptors
Set of pointers.
const PointerDescriptors & dataPointers() const
Property: Data pointers.
bool ignoreStrangeSizes
Whether to ignore strange-sized pointers.
void analyzeFunction(const Partitioner2::Partitioner &, const Sawyer::SharedPointer< Partitioner2::Function > &)
Analyze one function.
bool hasResults() const
Whether a function has been analyzed.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50