ROSE  0.11.66.0
Modules.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_Modules_H
2 #define ROSE_BinaryAnalysis_Partitioner2_Modules_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Partitioner2/BasicBlock.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/ControlFlowGraph.h>
9 #include <Rose/BinaryAnalysis/Partitioner2/Function.h>
10 #include <Rose/BinaryAnalysis/Partitioner2/Thunk.h>
11 #include <Rose/BinaryAnalysis/Partitioner2/Utility.h>
12 
13 #include <boost/logic/tribool.hpp>
14 #include <Sawyer/SharedPointer.h>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 namespace Partitioner2 {
19 
43 public:
46 
48  enum Termination {
52  };
53 
55  struct Results {
57  Results(): terminate(CONTINUE_DISCOVERY) {}
58  };
59 
61  struct Args {
65  Args(const Partitioner &partitioner, const BasicBlock::Ptr &bblock, Results &results)
66  : partitioner(partitioner), bblock(bblock), results(results) {}
67  };
68 
74  virtual bool operator()(bool chain, const Args&) = 0;
75 };
76 
82 public:
85 
96  virtual bool match(const Partitioner&, rose_addr_t anchor) = 0;
97 };
98 
99 
112 public:
115 
128  virtual std::vector<Function::Ptr> functions() const = 0;
129 };
130 
131 
138 public:
141 
148  virtual rose_addr_t match(const Partitioner&, rose_addr_t anchor) = 0;
149 };
150 
152 // Generic modules
154 
156 namespace Modules {
157 
166 std::string canonicalFunctionName(const std::string&);
167 
173 
185 public:
186  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
187  virtual bool operator()(bool chain, const Args &args) override;
188 };
189 
197 public:
198  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
199  virtual bool operator()(bool chain, const Args &args) override;
200 };
201 
209  size_t maxInsns_;
210 
211 protected:
212  BasicBlockSizeLimiter(size_t maxInsns)
213  : maxInsns_(maxInsns) {}
214 
215 public:
218 
220  static Ptr instance(size_t maxInsns) {
221  return Ptr(new BasicBlockSizeLimiter(maxInsns));
222  }
223 
229  size_t maxInstructions() const { return maxInsns_; }
230  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
233  virtual bool operator()(bool chain, const Args&) override;
234 };
235 
241 public:
243  typedef std::pair<rose_addr_t, rose_addr_t> AddressPair;
244 
245 private:
246  std::vector<AddressPair> rewrites_;
247 
248 protected:
249  IpRewriter(const std::vector<AddressPair> &rewrites)
250  : rewrites_(rewrites) {}
251 
252 public:
255 
257  static Ptr instance(const std::vector<AddressPair> &rewrites) {
258  return Ptr(new IpRewriter(rewrites));
259  }
260 
264  const std::vector<AddressPair>& rewrites() const { return rewrites_; }
265  std::vector<AddressPair>& rewrites() { return rewrites_; }
266  void rewrites(std::vector<AddressPair> &v) { rewrites_ = v; }
269  virtual bool operator()(bool chain, const Args&) override;
270 };
271 
276 public:
277  struct Settings {
278  AddressInterval where; // which basic block(s) we should we monitor
279  Trigger::Settings when; // once found, how often we produce a list
280  AddressInterval what; // what instructions to list
281  Settings(): what(AddressInterval::whole()) {}
282  };
283 private:
284  Settings settings_;
285  Trigger trigger_;
286 protected:
287  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
288 public:
289  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
290  static Ptr instance(const std::string &config);
291  static Ptr instance(const std::vector<std::string> &args);
292  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
293  static std::string docString();
294  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
295  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
296 };
297 
302 public:
303  struct Settings {
304  AddressInterval where; // what basic block(s) we should monitor (those starting within)
305  Trigger::Settings when; // once found, which event triggers the output
306  AddressInterval what; // which basic blocks should be in the output
307  bool showNeighbors; // should neighbor blocks be included in the output?
308  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
309  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
310  };
311 private:
312  Settings settings_;
313  Trigger trigger_;
314 protected:
315  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
316 public:
317  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
318  static Ptr instance(const std::string &config);
319  static Ptr instance(const std::vector<std::string> &args);
320  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
321  static std::string docString();
322  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
323  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
324 };
325 
330 public:
331  struct Settings {
332  AddressInterval where; // what basic block(s) we should monitor (those starting within)
333  Trigger::Settings when; // once found, which event triggers the output
334  AddressInterval what; // which bytes should be in the output
335  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
336  Settings(): accentSpecialValues(true) {}
337  };
338 private:
339  Settings settings_;
340  Trigger trigger_;
341 protected:
342  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
343 public:
344  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
345  static Ptr instance(const std::string &config);
346  static Ptr instance(const std::vector<std::string> &args);
347  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
348  static std::string docString();
349  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
350  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
351 };
352 
357 public:
358  struct Settings {
359  AddressInterval where; // what basic block(s) should we monitor (those starting within)
360  Trigger::Settings when; // once found, which event triggers
361  };
362 private:
363  Settings settings_;
364  Trigger trigger_;
365 protected:
366  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
367 public:
368  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
369  static Ptr instance(const std::string &config);
370  static Ptr instance(const std::vector<std::string> &args);
371  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
372  static std::string docString();
373  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
374  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
375  void debug(rose_addr_t, const BasicBlock::Ptr&);
376 };
377 
383 private:
384  ThunkPredicates::Ptr predicates_;
385 protected:
386  std::vector<Function::Ptr> functions_;
387 
388 protected:
389  // use 'instance' instead
391  : predicates_(predicates) {}
392 
393 public:
395  static Ptr instance(const ThunkPredicates::Ptr &predicates) {
396  return Ptr(new MatchThunk(predicates));
397  }
398 
402  ThunkPredicates::Ptr predicates() const { return predicates_; }
403  void predicates(const ThunkPredicates::Ptr &p) { predicates_ = p; }
406  virtual std::vector<Function::Ptr> functions() const override { return functions_; }
407  virtual bool match(const Partitioner&, rose_addr_t anchor) override;
408 };
409 
424 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
425  size_t leaveAtFront=16, size_t leaveAtBack=1);
426 
444 void nameStrings(const Partitioner&, const AddressInterval&);
445 
452 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*);
453 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmInterpretation*);
454 size_t findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*, std::vector<Function::Ptr>&);
461 void nameConstants(const Partitioner&);
462 
466 std::vector<Function::Ptr> findNoopFunctions(const Partitioner&);
467 
474 void nameNoopFunctions(const Partitioner&);
475 
482 boost::logic::tribool isStackBasedReturn(const Partitioner&, const BasicBlock::Ptr&);
483 
485 // Partitioner conversion to AST
487 
493 
499 
506 
512 
521 SgAsmBlock* buildAst(const Partitioner&, SgAsmInterpretation *interp=NULL,
523 
532 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
533 
543 
544 } // namespace
545 
546 } // namespace
547 } // namespace
548 } // namespace
549 
550 #endif
551 #endif
Termination
Whether to terminate a basic block.
Definition: Modules.h:48
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
Produce a hexdump at a certain time.
Definition: Modules.h:329
List some instructions at a certain time.
Definition: Modules.h:275
void nameConstants(const Partitioner &)
Gives names to constants in instructions.
void demangleFunctionNames(const Partitioner &)
Demangle all function names.
Base class for matching function padding.
Definition: Modules.h:137
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:42
void nameNoopFunctions(const Partitioner &)
Give names to functions that are no-ops.
std::pair< rose_addr_t, rose_addr_t > AddressPair
Pairs of old and new addresses.
Definition: Modules.h:243
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition: Modules.h:217
Base class for matching function prologues.
Definition: Modules.h:111
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:220
SgAsmBlock * buildAst(const Partitioner &, SgAsmInterpretation *interp=NULL, const AstConstructionSettings &settings=AstConstructionSettings::strict())
Builds an AST from the CFG.
SgAsmBlock * buildBasicBlockAst(const Partitioner &, const BasicBlock::Ptr &, const Function::Ptr &, const AstConstructionSettings &)
Build AST for basic block.
virtual bool operator()(bool chain, const Args &) override
Callback method.
std::string canonicalFunctionName(const std::string &)
Convert system function names to ROSE canonical form.
SgAsmBlock * buildDataBlockAst(const Partitioner &, const DataBlock::Ptr &, const AstConstructionSettings &)
Build AST for data block.
size_t maxInstructions() const
Property: Maximum size of block.
Definition: Modules.h:229
std::vector< AddressPair > & rewrites()
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:265
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:56
A collection of related switch declarations.
Represents a synthesized function.
void rewrites(std::vector< AddressPair > &v)
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:266
ThunkPredicates::Ptr predicates() const
Property: Predicates used for matching thunks.
Definition: Modules.h:402
static Ptr instance(const ThunkPredicates::Ptr &predicates)
Allocating constructor.
Definition: Modules.h:395
Results coordinated across all callbacks.
Definition: Modules.h:55
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:323
std::vector< Function::Ptr > findSymbolFunctions(const Partitioner &, SgAsmGenericHeader *)
Finds functions for which symbols exist.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
SgAsmFunction * buildFunctionAst(const Partitioner &, const Function::Ptr &, const AstConstructionSettings &)
Build AST for function.
Main namespace for the ROSE library.
Base class for matching an instruction pattern.
Definition: Modules.h:81
static AstConstructionSettings strict()
Default strict settings.
Sawyer::SharedPointer< CfgAdjustmentCallback > Ptr
Shared ownership pointer to CfgAdjustmentCallback.
Make current instruction the final instruction of the block.
Definition: Modules.h:50
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:350
Convenient place to attach a debugger.
Definition: Modules.h:356
static Ptr instance(const std::vector< AddressPair > &rewrites)
Constructor.
Definition: Modules.h:257
Trigger based on number of times called.
Definition: Utility.h:193
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:374
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
Base class for container file headers.
virtual bool operator()(bool chain, const Args &)=0
Callback method.
virtual bool match(const Partitioner &, rose_addr_t anchor)=0
Attempt to match an instruction pattern.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
virtual bool operator()(bool chain, const Args &args) override
Callback method.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9627
static Interval whole()
Construct an interval that covers the entire domain.
Definition: Interval.h:180
virtual rose_addr_t match(const Partitioner &, rose_addr_t anchor)=0
Attempt to match padding.
const Partitioner & partitioner
Partitioner requesting basic block successors.
Definition: Modules.h:62
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:230
virtual std::vector< Function::Ptr > functions() const =0
Returns the function(s) for the previous successful match.
AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map, size_t threshold, size_t leaveAtFront=16, size_t leaveAtBack=1)
Remove execute permissions for zeros.
const std::vector< AddressPair > & rewrites() const
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:264
SgAsmBlock * buildGlobalBlockAst(const Partitioner &, const AstConstructionSettings &)
Builds the global block AST.
virtual bool operator()(bool chain, const Args &args) override
Callback method.
BasicBlock::Ptr bblock
Basic block whose successors are to be computed.
Definition: Modules.h:63
Sawyer::SharedPointer< FunctionPaddingMatcher > Ptr
Shared-ownership pointer to a FunctionPaddingMatcher.
Definition: Modules.h:140
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:301
Base class for reference counted objects.
Definition: SharedObject.h:64
Make previous instruction the final instruction of the block.
Definition: Modules.h:51
void labelSymbolAddresses(Partitioner &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
Sawyer::SharedPointer< FunctionPrologueMatcher > Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition: Modules.h:114
virtual bool operator()(bool chain, const Args &) override
Callback method.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:84
Sawyer::SharedPointer< BasicBlockCallback > Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:45
void nameStrings(const Partitioner &, const AddressInterval &)
Give labels to string constants.
virtual std::vector< Function::Ptr > functions() const override
Returns the function(s) for the previous successful match.
Definition: Modules.h:406
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
virtual bool match(const Partitioner &, rose_addr_t anchor) override
Attempt to match an instruction pattern.
void predicates(const ThunkPredicates::Ptr &p)
Property: Predicates used for matching thunks.
Definition: Modules.h:403
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:64
void fixupAstCallingConventions(const Partitioner &, SgNode *ast)
Fixes calling convention results.
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:295
boost::logic::tribool isStackBasedReturn(const Partitioner &, const BasicBlock::Ptr &)
Determine if basic block is a stack-based function return.
std::vector< Function::Ptr > findNoopFunctions(const Partitioner &)
Find functions that are no-ops.
Sawyer::SharedPointer< IpRewriter > Ptr
Shared-ownership pointer to a IpRewriter.
Definition: Modules.h:254