ROSE  0.9.9.139
Modules.h
1 #ifndef ROSE_Partitioner2_Modules_H
2 #define ROSE_Partitioner2_Modules_H
3 
4 #include <Partitioner2/BasicBlock.h>
5 #include <Partitioner2/BasicTypes.h>
6 #include <Partitioner2/ControlFlowGraph.h>
7 #include <Partitioner2/Function.h>
8 #include <Partitioner2/Utility.h>
9 
10 #include <Sawyer/SharedPointer.h>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 namespace Partitioner2 {
15 
39 public:
42 
44  enum Termination {
48  };
49 
51  struct Results {
53  Results(): terminate(CONTINUE_DISCOVERY) {}
54  };
55 
57  struct Args {
61  Args(const Partitioner &partitioner, const BasicBlock::Ptr &bblock, Results &results)
62  : partitioner(partitioner), bblock(bblock), results(results) {}
63  };
64 
70  virtual bool operator()(bool chain, const Args&) = 0;
71 };
72 
78 public:
81 
92  virtual bool match(const Partitioner&, rose_addr_t anchor) = 0;
93 };
94 
95 
108 public:
111 
124  virtual std::vector<Function::Ptr> functions() const = 0;
125 };
126 
127 
134 public:
137 
144  virtual rose_addr_t match(const Partitioner&, rose_addr_t anchor) = 0;
145 };
146 
148 // Generic modules
150 
151 namespace Modules {
152 
161 std::string canonicalFunctionName(const std::string&);
162 
167 void demangleFunctionNames(const Partitioner&);
168 
180 public:
181  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
182  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
183 };
184 
192 public:
193  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
194  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
195 };
196 
204  size_t maxInsns_;
205 
206 protected:
207  BasicBlockSizeLimiter(size_t maxInsns)
208  : maxInsns_(maxInsns) {}
209 
210 public:
213 
215  static Ptr instance(size_t maxInsns) {
216  return Ptr(new BasicBlockSizeLimiter(maxInsns));
217  }
218 
224  size_t maxInstructions() const { return maxInsns_; }
225  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
228  virtual bool operator()(bool chain, const Args&) ROSE_OVERRIDE;
229 };
230 
235 public:
236  struct Settings {
237  AddressInterval where; // which basic block(s) we should we monitor
238  Trigger::Settings when; // once found, how often we produce a list
239  AddressInterval what; // what instructions to list
240  Settings(): what(AddressInterval::whole()) {}
241  };
242 private:
243  Settings settings_;
244  Trigger trigger_;
245 protected:
246  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
247 public:
248  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
249  static Ptr instance(const std::string &config);
250  static Ptr instance(const std::vector<std::string> &args);
251  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
252  static std::string docString();
253  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
254  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
255 };
256 
260 class CfgGraphVizDumper: public CfgAdjustmentCallback {
261 public:
262  struct Settings {
263  AddressInterval where; // what basic block(s) we should monitor (those starting within)
264  Trigger::Settings when; // once found, which event triggers the output
265  AddressInterval what; // which basic blocks should be in the output
266  bool showNeighbors; // should neighbor blocks be included in the output?
267  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
268  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
269  };
270 private:
271  Settings settings_;
272  Trigger trigger_;
273 protected:
274  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
275 public:
276  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
277  static Ptr instance(const std::string &config);
278  static Ptr instance(const std::vector<std::string> &args);
279  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
280  static std::string docString();
281  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
282  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
283 };
284 
288 class HexDumper: public CfgAdjustmentCallback {
289 public:
290  struct Settings {
291  AddressInterval where; // what basic block(s) we should monitor (those starting within)
292  Trigger::Settings when; // once found, which event triggers the output
293  AddressInterval what; // which bytes should be in the output
294  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
295  Settings(): accentSpecialValues(true) {}
296  };
297 private:
298  Settings settings_;
299  Trigger trigger_;
300 protected:
301  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
302 public:
303  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
304  static Ptr instance(const std::string &config);
305  static Ptr instance(const std::vector<std::string> &args);
306  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
307  static std::string docString();
308  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
309  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
310 };
311 
315 class Debugger: public CfgAdjustmentCallback {
316 public:
317  struct Settings {
318  AddressInterval where; // what basic block(s) should we monitor (those starting within)
319  Trigger::Settings when; // once found, which event triggers
320  };
321 private:
322  Settings settings_;
323  Trigger trigger_;
324 protected:
325  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
326 public:
327  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
328  static Ptr instance(const std::string &config);
329  static Ptr instance(const std::vector<std::string> &args);
330  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
331  static std::string docString();
332  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
333  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
334  void debug(rose_addr_t, const BasicBlock::Ptr&);
335 };
336 
351 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
352  size_t leaveAtFront=16, size_t leaveAtBack=1);
353 
361 void labelSymbolAddresses(Partitioner&, SgAsmGenericHeader*);
362 void labelSymbolAddresses(Partitioner&, SgAsmInterpretation*);
370 void nameStrings(const Partitioner&);
371 
378 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*);
379 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmInterpretation*);
380 size_t findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*, std::vector<Function::Ptr>&);
387 void nameConstants(const Partitioner&);
388 
392 std::vector<Function::Ptr> findNoopFunctions(const Partitioner&);
393 
400 void nameNoopFunctions(const Partitioner&);
401 
402 
404 // Partitioner conversion to AST
406 
411 SgAsmBlock* buildBasicBlockAst(const Partitioner&, const BasicBlock::Ptr&, const Function::Ptr&, const AstConstructionSettings&);
412 
417 SgAsmBlock* buildDataBlockAst(const Partitioner&, const DataBlock::Ptr&, const AstConstructionSettings&);
418 
424 SgAsmFunction* buildFunctionAst(const Partitioner&, const Function::Ptr&, const AstConstructionSettings&);
425 
430 SgAsmBlock* buildGlobalBlockAst(const Partitioner&, const AstConstructionSettings&);
431 
440 SgAsmBlock* buildAst(const Partitioner&, SgAsmInterpretation *interp=NULL,
442 
451 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
452 
461 void fixupAstCallingConventions(const Partitioner&, SgNode *ast);
462 
463 } // namespace
464 
465 } // namespace
466 } // namespace
467 } // namespace
468 
469 #endif
Termination
Whether to terminate a basic block.
Definition: Modules.h:44
Produce a hexdump at a certain time.
Definition: Modules.h:288
List some instructions at a certain time.
Definition: Modules.h:234
Base class for matching function padding.
Definition: Modules.h:133
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:38
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition: Modules.h:212
Base class for matching function prologues.
Definition: Modules.h:107
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:215
virtual bool operator()(bool chain, const Args &) ROSE_OVERRIDE
Callback method.
size_t maxInstructions() const
Property: Maximum size of block.
Definition: Modules.h:224
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:52
A collection of related switch declarations.
Definition: CommandLine.h:2488
Represents a synthesized function.
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:333
Results coordinated across all callbacks.
Definition: Modules.h:51
virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE
Callback method.
Main namespace for the ROSE library.
Settings that control building the AST.
Definition: BasicTypes.h:69
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:309
Base class for matching an instruction pattern.
Definition: Modules.h:77
static AstConstructionSettings strict()
Default strict settings.
Definition: BasicTypes.h:122
Make current instruction the final instruction of the block.
Definition: Modules.h:46
Reference-counting smart pointer.
Definition: SharedPointer.h:34
Convenient place to attach a debugger.
Definition: Modules.h:315
Trigger based on number of times called.
Definition: Utility.h:180
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:8322
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:58
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:225
virtual std::vector< Function::Ptr > functions() const =0
Returns the function(s) for the previous successful match.
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:254
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:59
Sawyer::SharedPointer< FunctionPaddingMatcher > Ptr
Shared-ownership pointer to a FunctionPaddingMatcher.
Definition: Modules.h:136
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:260
Base class for reference counted objects.
Definition: SharedObject.h:22
Make previous instruction the final instruction of the block.
Definition: Modules.h:47
Sawyer::SharedPointer< FunctionPrologueMatcher > Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition: Modules.h:110
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:80
Sawyer::SharedPointer< BasicBlockCallback > Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:41
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:282
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:290
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:60