ROSE  0.10.5.0
Modules.h
1 #ifndef ROSE_Partitioner2_Modules_H
2 #define ROSE_Partitioner2_Modules_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Partitioner2/BasicBlock.h>
8 #include <Partitioner2/BasicTypes.h>
9 #include <Partitioner2/ControlFlowGraph.h>
10 #include <Partitioner2/Function.h>
11 #include <Partitioner2/Thunk.h>
12 #include <Partitioner2/Utility.h>
13 
14 #include <boost/logic/tribool.hpp>
15 #include <Sawyer/SharedPointer.h>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 namespace Partitioner2 {
20 
44 public:
47 
49  enum Termination {
53  };
54 
56  struct Results {
58  Results(): terminate(CONTINUE_DISCOVERY) {}
59  };
60 
62  struct Args {
66  Args(const Partitioner &partitioner, const BasicBlock::Ptr &bblock, Results &results)
67  : partitioner(partitioner), bblock(bblock), results(results) {}
68  };
69 
75  virtual bool operator()(bool chain, const Args&) = 0;
76 };
77 
83 public:
86 
97  virtual bool match(const Partitioner&, rose_addr_t anchor) = 0;
98 };
99 
100 
113 public:
116 
129  virtual std::vector<Function::Ptr> functions() const = 0;
130 };
131 
132 
139 public:
142 
149  virtual rose_addr_t match(const Partitioner&, rose_addr_t anchor) = 0;
150 };
151 
153 // Generic modules
155 
157 namespace Modules {
158 
167 std::string canonicalFunctionName(const std::string&);
168 
174 
186 public:
187  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
188  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
189 };
190 
198 public:
199  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
200  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
201 };
202 
210  size_t maxInsns_;
211 
212 protected:
213  BasicBlockSizeLimiter(size_t maxInsns)
214  : maxInsns_(maxInsns) {}
215 
216 public:
219 
221  static Ptr instance(size_t maxInsns) {
222  return Ptr(new BasicBlockSizeLimiter(maxInsns));
223  }
224 
230  size_t maxInstructions() const { return maxInsns_; }
231  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
234  virtual bool operator()(bool chain, const Args&) ROSE_OVERRIDE;
235 };
236 
242 public:
244  typedef std::pair<rose_addr_t, rose_addr_t> AddressPair;
245 
246 private:
247  std::vector<AddressPair> rewrites_;
248 
249 protected:
250  IpRewriter(const std::vector<AddressPair> &rewrites)
251  : rewrites_(rewrites) {}
252 
253 public:
256 
258  static Ptr instance(const std::vector<AddressPair> &rewrites) {
259  return Ptr(new IpRewriter(rewrites));
260  }
261 
265  const std::vector<AddressPair>& rewrites() const { return rewrites_; }
266  std::vector<AddressPair>& rewrites() { return rewrites_; }
267  void rewrites(std::vector<AddressPair> &v) { rewrites_ = v; }
270  virtual bool operator()(bool chain, const Args&) ROSE_OVERRIDE;
271 };
272 
277 public:
278  struct Settings {
279  AddressInterval where; // which basic block(s) we should we monitor
280  Trigger::Settings when; // once found, how often we produce a list
281  AddressInterval what; // what instructions to list
282  Settings(): what(AddressInterval::whole()) {}
283  };
284 private:
285  Settings settings_;
286  Trigger trigger_;
287 protected:
288  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
289 public:
290  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
291  static Ptr instance(const std::string &config);
292  static Ptr instance(const std::vector<std::string> &args);
293  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
294  static std::string docString();
295  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
296  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
297 };
298 
302 class CfgGraphVizDumper: public CfgAdjustmentCallback {
303 public:
304  struct Settings {
305  AddressInterval where; // what basic block(s) we should monitor (those starting within)
306  Trigger::Settings when; // once found, which event triggers the output
307  AddressInterval what; // which basic blocks should be in the output
308  bool showNeighbors; // should neighbor blocks be included in the output?
309  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
310  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
311  };
312 private:
313  Settings settings_;
314  Trigger trigger_;
315 protected:
316  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
317 public:
318  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
319  static Ptr instance(const std::string &config);
320  static Ptr instance(const std::vector<std::string> &args);
321  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
322  static std::string docString();
323  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
324  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
325 };
326 
330 class HexDumper: public CfgAdjustmentCallback {
331 public:
332  struct Settings {
333  AddressInterval where; // what basic block(s) we should monitor (those starting within)
334  Trigger::Settings when; // once found, which event triggers the output
335  AddressInterval what; // which bytes should be in the output
336  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
337  Settings(): accentSpecialValues(true) {}
338  };
339 private:
340  Settings settings_;
341  Trigger trigger_;
342 protected:
343  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
344 public:
345  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
346  static Ptr instance(const std::string &config);
347  static Ptr instance(const std::vector<std::string> &args);
348  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
349  static std::string docString();
350  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
351  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
352 };
353 
357 class Debugger: public CfgAdjustmentCallback {
358 public:
359  struct Settings {
360  AddressInterval where; // what basic block(s) should we monitor (those starting within)
361  Trigger::Settings when; // once found, which event triggers
362  };
363 private:
364  Settings settings_;
365  Trigger trigger_;
366 protected:
367  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
368 public:
369  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
370  static Ptr instance(const std::string &config);
371  static Ptr instance(const std::vector<std::string> &args);
372  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
373  static std::string docString();
374  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
375  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
376  void debug(rose_addr_t, const BasicBlock::Ptr&);
377 };
378 
384 private:
385  ThunkPredicates::Ptr predicates_;
386 protected:
387  std::vector<Function::Ptr> functions_;
388 
389 protected:
390  // use 'instance' instead
391  MatchThunk(const ThunkPredicates::Ptr &predicates)
392  : predicates_(predicates) {}
393 
394 public:
396  static Ptr instance(const ThunkPredicates::Ptr &predicates) {
397  return Ptr(new MatchThunk(predicates));
398  }
399 
403  ThunkPredicates::Ptr predicates() const { return predicates_; }
404  void predicates(const ThunkPredicates::Ptr &p) { predicates_ = p; }
407  virtual std::vector<Function::Ptr> functions() const ROSE_OVERRIDE { return functions_; }
408  virtual bool match(const Partitioner&, rose_addr_t anchor) ROSE_OVERRIDE;
409 };
410 
425 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
426  size_t leaveAtFront=16, size_t leaveAtBack=1);
427 
444 void nameStrings(const Partitioner&);
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:49
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
Produce a hexdump at a certain time.
Definition: Modules.h:330
List some instructions at a certain time.
Definition: Modules.h:276
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:138
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:43
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:244
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition: Modules.h:218
Base class for matching function prologues.
Definition: Modules.h:112
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:221
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.
std::string canonicalFunctionName(const std::string &)
Convert system function names to ROSE canonical form.
virtual std::vector< Function::Ptr > functions() const ROSE_OVERRIDE
Returns the function(s) for the previous successful match.
Definition: Modules.h:407
SgAsmBlock * buildDataBlockAst(const Partitioner &, const DataBlock::Ptr &, const AstConstructionSettings &)
Build AST for data block.
virtual bool operator()(bool chain, const Args &) ROSE_OVERRIDE
Callback method.
size_t maxInstructions() const
Property: Maximum size of block.
Definition: Modules.h:230
std::vector< AddressPair > & rewrites()
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:266
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:57
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:267
ThunkPredicates::Ptr predicates() const
Property: Predicates used for matching thunks.
Definition: Modules.h:403
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:375
static Ptr instance(const ThunkPredicates::Ptr &predicates)
Allocating constructor.
Definition: Modules.h:396
Results coordinated across all callbacks.
Definition: Modules.h:56
std::vector< Function::Ptr > findSymbolFunctions(const Partitioner &, SgAsmGenericHeader *)
Finds functions for which symbols exist.
virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE
Callback method.
SgAsmFunction * buildFunctionAst(const Partitioner &, const Function::Ptr &, const AstConstructionSettings &)
Build AST for function.
Main namespace for the ROSE library.
Settings that control building the AST.
Definition: BasicTypes.h:94
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:351
Base class for matching an instruction pattern.
Definition: Modules.h:82
static AstConstructionSettings strict()
Default strict settings.
Definition: BasicTypes.h:150
Make current instruction the final instruction of the block.
Definition: Modules.h:51
Reference-counting smart pointer.
Definition: SharedPointer.h:67
Convenient place to attach a debugger.
Definition: Modules.h:357
static Ptr instance(const std::vector< AddressPair > &rewrites)
Constructor.
Definition: Modules.h:258
Trigger based on number of times called.
Definition: Utility.h:205
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.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9176
static Interval whole()
Construct an interval that covers the entire domain.
Definition: Interval.h:167
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:63
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:231
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.
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:296
const std::vector< AddressPair > & rewrites() const
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:265
SgAsmBlock * buildGlobalBlockAst(const Partitioner &, const AstConstructionSettings &)
Builds the global block AST.
virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE
Callback method.
BasicBlock::Ptr bblock
Basic block whose successors are to be computed.
Definition: Modules.h:64
Sawyer::SharedPointer< FunctionPaddingMatcher > Ptr
Shared-ownership pointer to a FunctionPaddingMatcher.
Definition: Modules.h:141
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:302
Base class for reference counted objects.
Definition: SharedObject.h:64
Make previous instruction the final instruction of the block.
Definition: Modules.h:52
void labelSymbolAddresses(Partitioner &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
Sawyer::SharedPointer< FunctionPrologueMatcher > Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition: Modules.h:115
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:85
Sawyer::SharedPointer< BasicBlockCallback > Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:46
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:324
void nameStrings(const Partitioner &)
Give labels to string constants.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:321
void predicates(const ThunkPredicates::Ptr &p)
Property: Predicates used for matching thunks.
Definition: Modules.h:404
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:65
void fixupAstCallingConventions(const Partitioner &, SgNode *ast)
Fixes calling convention results.
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:255