ROSE  0.11.145.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 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
6 
7 #include <Rose/BinaryAnalysis/Partitioner2/ControlFlowGraph.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/Thunk.h>
9 
10 #include <boost/logic/tribool.hpp>
11 #include <Sawyer/SharedPointer.h>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace Partitioner2 {
16 
40 public:
43 
45  enum Termination {
49  };
50 
52  struct Results {
54  Results(): terminate(CONTINUE_DISCOVERY) {}
55  };
56 
58  struct Args {
63  ~Args();
64  };
65 
71  virtual bool operator()(bool chain, const Args&) = 0;
72 };
73 
79 public:
82 
93  virtual bool match(const PartitionerConstPtr&, rose_addr_t anchor) = 0;
94 };
95 
96 
109 public:
112 
125  virtual std::vector<FunctionPtr> functions() const = 0;
126 };
127 
128 
135 public:
138 
145  virtual rose_addr_t match(const PartitionerConstPtr&, rose_addr_t anchor) = 0;
146 };
147 
149 // Generic modules
151 
153 namespace Modules {
154 
163 std::string canonicalFunctionName(const std::string&);
164 
170 
182 public:
183  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
184  virtual bool operator()(bool chain, const Args &args) override;
185 };
186 
194 public:
195  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
196  virtual bool operator()(bool chain, const Args &args) override;
197 };
198 
206  size_t maxInsns_;
207 
208 protected:
209  BasicBlockSizeLimiter(size_t maxInsns)
210  : maxInsns_(maxInsns) {}
211 
212 public:
215 
217  static Ptr instance(size_t maxInsns) {
218  return Ptr(new BasicBlockSizeLimiter(maxInsns));
219  }
220 
226  size_t maxInstructions() const { return maxInsns_; }
227  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
230  virtual bool operator()(bool chain, const Args&) override;
231 };
232 
238 public:
240  typedef std::pair<rose_addr_t, rose_addr_t> AddressPair;
241 
242 private:
243  std::vector<AddressPair> rewrites_;
244 
245 protected:
246  IpRewriter(const std::vector<AddressPair> &rewrites)
247  : rewrites_(rewrites) {}
248 
249 public:
252 
254  static Ptr instance(const std::vector<AddressPair> &rewrites) {
255  return Ptr(new IpRewriter(rewrites));
256  }
257 
261  const std::vector<AddressPair>& rewrites() const { return rewrites_; }
262  std::vector<AddressPair>& rewrites() { return rewrites_; }
263  void rewrites(std::vector<AddressPair> &v) { rewrites_ = v; }
266  virtual bool operator()(bool chain, const Args&) override;
267 };
268 
273 public:
274  struct Settings {
275  AddressInterval where; // which basic block(s) we should we monitor
276  Trigger::Settings when; // once found, how often we produce a list
277  AddressInterval what; // what instructions to list
278  Settings(): what(AddressInterval::whole()) {}
279  };
280 private:
281  Settings settings_;
282  Trigger trigger_;
283 protected:
284  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
285 public:
286  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
287  static Ptr instance(const std::string &config);
288  static Ptr instance(const std::vector<std::string> &args);
289  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
290  static std::string docString();
291  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
292  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
293 };
294 
299 public:
300  struct Settings {
301  AddressInterval where; // what basic block(s) we should monitor (those starting within)
302  Trigger::Settings when; // once found, which event triggers the output
303  AddressInterval what; // which basic blocks should be in the output
304  bool showNeighbors; // should neighbor blocks be included in the output?
305  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
306  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
307  };
308 private:
309  Settings settings_;
310  Trigger trigger_;
311 protected:
312  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
313 public:
314  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
315  static Ptr instance(const std::string &config);
316  static Ptr instance(const std::vector<std::string> &args);
317  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
318  static std::string docString();
319  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
320  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
321 };
322 
327 public:
328  struct Settings {
329  AddressInterval where; // what basic block(s) we should monitor (those starting within)
330  Trigger::Settings when; // once found, which event triggers the output
331  AddressInterval what; // which bytes should be in the output
332  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
333  Settings(): accentSpecialValues(true) {}
334  };
335 private:
336  Settings settings_;
337  Trigger trigger_;
338 protected:
339  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
340 public:
341  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
342  static Ptr instance(const std::string &config);
343  static Ptr instance(const std::vector<std::string> &args);
344  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
345  static std::string docString();
346  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
347  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
348 };
349 
354 public:
355  struct Settings {
356  AddressInterval where; // what basic block(s) should we monitor (those starting within)
357  Trigger::Settings when; // once found, which event triggers
358  };
359 private:
360  Settings settings_;
361  Trigger trigger_;
362 protected:
363  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
364 public:
365  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
366  static Ptr instance(const std::string &config);
367  static Ptr instance(const std::vector<std::string> &args);
368  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
369  static std::string docString();
370  virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
371  virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
372  void debug(rose_addr_t, const BasicBlockPtr&);
373 };
374 
380 private:
381  ThunkPredicatesPtr predicates_;
382 protected:
383  std::vector<FunctionPtr> functions_;
384 
385 protected:
386  // use 'instance' instead
388 public:
389  ~MatchThunk();
390 
391 public:
393  static Ptr instance(const ThunkPredicatesPtr&);
394 
399  void predicates(const ThunkPredicatesPtr&);
402  virtual std::vector<FunctionPtr> functions() const override;
403  virtual bool match(const PartitionerConstPtr&, rose_addr_t anchor) override;
404 };
405 
420 AddressIntervalSet deExecuteZeros(const MemoryMapPtr &map /*in,out*/, size_t threshold,
421  size_t leaveAtFront=16, size_t leaveAtBack=1);
422 
440 void nameStrings(const PartitionerConstPtr&, const AddressInterval&);
441 
448 std::vector<FunctionPtr> findSymbolFunctions(const PartitionerConstPtr&, SgAsmGenericHeader*);
449 std::vector<FunctionPtr> findSymbolFunctions(const PartitionerConstPtr&, SgAsmInterpretation*);
450 size_t findSymbolFunctions(const PartitionerConstPtr&, SgAsmGenericHeader*, std::vector<FunctionPtr>&);
458 
462 std::vector<FunctionPtr> findNoopFunctions(const PartitionerConstPtr&);
463 
471 
478 boost::logic::tribool isStackBasedReturn(const PartitionerConstPtr&, const BasicBlockPtr&);
479 
481 // Partitioner conversion to AST
483 
489  const AstConstructionSettings&);
490 
496 
503 
509 
520 
529 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
530 
540 
541 } // namespace
542 
543 } // namespace
544 } // namespace
545 } // namespace
546 
547 #endif
548 #endif
Termination
Whether to terminate a basic block.
Definition: Modules.h:45
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
Produce a hexdump at a certain time.
Definition: Modules.h:326
void nameConstants(const PartitionerConstPtr &, const AddressInterval &)
Gives names to constants in instructions.
List some instructions at a certain time.
Definition: Modules.h:272
std::vector< FunctionPtr > findSymbolFunctions(const PartitionerConstPtr &, SgAsmGenericHeader *)
Finds functions for which symbols exist.
Base class for matching function padding.
Definition: Modules.h:134
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:39
std::pair< rose_addr_t, rose_addr_t > AddressPair
Pairs of old and new addresses.
Definition: Modules.h:240
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition: Modules.h:214
virtual bool match(const PartitionerConstPtr &, rose_addr_t anchor)=0
Attempt to match an instruction pattern.
Base class for matching function prologues.
Definition: Modules.h:108
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:217
PartitionerConstPtr partitioner
Partitioner requesting basic block successors.
Definition: Modules.h:59
virtual bool operator()(bool chain, const Args &) override
Callback method.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
std::string canonicalFunctionName(const std::string &)
Convert system function names to ROSE canonical form.
AddressIntervalSet deExecuteZeros(const MemoryMapPtr &map, size_t threshold, size_t leaveAtFront=16, size_t leaveAtBack=1)
Remove execute permissions for zeros.
Sawyer::SharedPointer< BasicBlockCallback > BasicBlockCallbackPtr
Shared ownership pointer.
size_t maxInstructions() const
Property: Maximum size of block.
Definition: Modules.h:226
std::vector< AddressPair > & rewrites()
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:262
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:53
static Ptr instance(const ThunkPredicatesPtr &)
Allocating constructor.
virtual bool match(const PartitionerConstPtr &, rose_addr_t anchor) override
Attempt to match an instruction pattern.
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:263
Results coordinated across all callbacks.
Definition: Modules.h:52
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:320
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
Main namespace for the ROSE library.
void fixupAstCallingConventions(const PartitionerConstPtr &, SgNode *ast)
Fixes calling convention results.
Base class for matching an instruction pattern.
Definition: Modules.h:78
static AstConstructionSettings strict()
Default strict settings.
Sawyer::SharedPointer< CfgAdjustmentCallback > Ptr
Shared ownership pointer.
Make current instruction the final instruction of the block.
Definition: Modules.h:47
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
SgAsmFunction * buildFunctionAst(const PartitionerConstPtr &, const FunctionPtr &, const AstConstructionSettings &)
Build AST for function.
SgAsmBlock * buildDataBlockAst(const PartitionerConstPtr &, const DataBlockPtr &, const AstConstructionSettings &)
Build AST for data block.
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:347
SgAsmBlock * buildAst(const PartitionerConstPtr &, SgAsmInterpretation *interp=NULL, const AstConstructionSettings &settings=AstConstructionSettings::strict())
Builds an AST from the CFG.
Convenient place to attach a debugger.
Definition: Modules.h:353
static Ptr instance(const std::vector< AddressPair > &rewrites)
Constructor.
Definition: Modules.h:254
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:371
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
Base class for container file headers.
boost::logic::tribool isStackBasedReturn(const PartitionerConstPtr &, const BasicBlockPtr &)
Determine if basic block is a stack-based function return.
virtual bool operator()(bool chain, const Args &)=0
Callback method.
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.
void nameStrings(const PartitionerConstPtr &, const AddressInterval &)
Give labels to string constants.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846
static Interval whole()
Construct an interval that covers the entire domain.
Definition: Interval.h:180
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:227
std::vector< FunctionPtr > findNoopFunctions(const PartitionerConstPtr &)
Find functions that are no-ops.
const std::vector< AddressPair > & rewrites() const
Property: Pairs of old/new addresses to rewrite.
Definition: Modules.h:261
virtual bool operator()(bool chain, const Args &args) override
Callback method.
virtual std::vector< FunctionPtr > functions() const =0
Returns the function(s) for the previous successful match.
Sawyer::SharedPointer< FunctionPaddingMatcher > FunctionPaddingMatcherPtr
Shared ownership pointer.
void nameNoopFunctions(const PartitionerConstPtr &)
Give names to functions that are no-ops.
Sawyer::SharedPointer< FunctionPrologueMatcher > FunctionPrologueMatcherPtr
Shared ownership pointer.
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:298
Base class for reference counted objects.
Definition: SharedObject.h:64
Make previous instruction the final instruction of the block.
Definition: Modules.h:48
SgAsmBlock * buildBasicBlockAst(const PartitionerConstPtr &, const BasicBlockPtr &, const FunctionPtr &, const AstConstructionSettings &)
Build AST for basic block.
virtual bool operator()(bool chain, const Args &) override
Callback method.
SgAsmBlock * buildGlobalBlockAst(const PartitionerConstPtr &, const AstConstructionSettings &)
Builds the global block AST.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
void labelSymbolAddresses(const PartitionerPtr &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:81
ThunkPredicatesPtr predicates() const
Property: Predicates used for matching thunks.
virtual rose_addr_t match(const PartitionerConstPtr &, rose_addr_t anchor)=0
Attempt to match padding.
BasicBlockPtr bblock
Basic block whose successors are to be computed.
Definition: Modules.h:60
BasicBlockCallbackPtr Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:42
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:61
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition: Modules.h:292
void demangleFunctionNames(const PartitionerConstPtr &)
Demangle all function names.
Sawyer::SharedPointer< IpRewriter > Ptr
Shared-ownership pointer to a IpRewriter.
Definition: Modules.h:251