ROSE  0.11.2.0
BinaryPointerDetection.h
1 #ifndef ROSE_BinaryAnalysis_PointerDetection_H
2 #define ROSE_BinaryAnalysis_PointerDetection_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <BaseSemantics2.h>
7 #include <MemoryCellList.h>
8 #include <Sawyer/Set.h>
9 
10 namespace Rose {
11 namespace BinaryAnalysis {
12 
13 // Forwards
14 class Disassembler;
15 namespace Partitioner2 {
16  class Partitioner;
17  class Function;
18 }
19 
20 
113 namespace PointerDetection {
114 
118 void initDiagnostics();
119 
124 
126 struct Settings {
133 
139 
142  : ignoreConstIp(true), ignoreStrangeSizes(true) {}
143 };
144 
148  size_t nBits;
150  PointerDescriptor(const SymbolicExpr::Ptr &lvalue, size_t nBits)
151  : lvalue(lvalue), nBits(nBits) {}
152 };
153 
156 public:
157  bool operator()(const PointerDescriptor &a, const PointerDescriptor &b);
158 };
159 
161 typedef std::set<PointerDescriptor, PointerDescriptorLessp> PointerDescriptors;
162 
167 class Analysis {
168 public:
169 
170 private:
171  Settings settings_;
173  bool hasResults_; // Are the following data members initialized?
174  bool didConverge_; // Are the following data members valid (else only appoximations)?
175  PointerDescriptors codePointers_; // Memory addresses that hold a pointer to code
176  PointerDescriptors dataPointers_; // Memory addresses that hold a pointer to data
177  InstructionSemantics2::BaseSemantics::StatePtr initialState_; // Initial state for analysis
178  InstructionSemantics2::BaseSemantics::StatePtr finalState_; // Final state for analysis
179 
180 public:
187  : hasResults_(false), didConverge_(false) {}
188 
192  explicit Analysis(Disassembler *d, const Settings &settings = Settings())
193  : hasResults_(false), didConverge_(false) {
194  init(d);
195  }
196 
204  const Settings &settings = Settings())
205  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
206 
210  const Settings& settings() const { return settings_; }
211 
218 
223  bool hasResults() const { return hasResults_; }
224 
229  bool didConverge() const { return didConverge_; }
230 
235  void clearResults();
236 
241  void clearNonResults();
242 
247  const PointerDescriptors& codePointers() const {
248  return codePointers_;
249  }
250 
255  const PointerDescriptors& dataPointers() const {
256  return dataPointers_;
257  }
258 
265  return initialState_;
266  }
267 
274  return finalState_;
275  }
276 
277 private:
278  void init(Disassembler*);
279 
281  makeRiscOperators(const Partitioner2::Partitioner&) const;
282 
283  // Prints instructions to the mlog[DEBUG] diagnostic stream if that stream is enabled.
284  void
285  printInstructionsForDebugging(const Partitioner2::Partitioner&, const Sawyer::SharedPointer<Partitioner2::Function>&);
286 
287  // Given a potential pointer's r-value, determine if the r-value is a pointer and if so, store its address in the
288  // result. The pointer's value and the defining instructions are added to the two sets, and the result is not updated for
289  // values and instructions that have already been processed.
290  void
291  conditionallySavePointer(const InstructionSemantics2::BaseSemantics::SValuePtr &ptrValue,
292  Sawyer::Container::Set<uint64_t> &ptrValueSeen,
293  size_t wordSize, PointerDescriptors &result);
294 };
295 
296 } // namespace
297 } // namespace
298 } // namespace
299 
300 #endif
301 #endif
Ordered set of values.
Definition: Set.h:46
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:1599
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.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:322
bool hasResults() const
Whether a function has been analyzed.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50