ROSE  0.9.11.56
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/Thunk.h>
9 #include <Partitioner2/Utility.h>
10 
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 {
62  Args(const Partitioner &partitioner, const BasicBlock::Ptr &bblock, Results &results)
63  : partitioner(partitioner), bblock(bblock), results(results) {}
64  };
65 
71  virtual bool operator()(bool chain, const Args&) = 0;
72 };
73 
79 public:
82 
93  virtual bool match(const Partitioner&, rose_addr_t anchor) = 0;
94 };
95 
96 
109 public:
112 
125  virtual std::vector<Function::Ptr> functions() const = 0;
126 };
127 
128 
135 public:
138 
145  virtual rose_addr_t match(const Partitioner&, 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) ROSE_OVERRIDE;
185 };
186 
194 public:
195  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
196  virtual bool operator()(bool chain, const Args &args) ROSE_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&) ROSE_OVERRIDE;
231 };
232 
237 public:
238  struct Settings {
239  AddressInterval where; // which basic block(s) we should we monitor
240  Trigger::Settings when; // once found, how often we produce a list
241  AddressInterval what; // what instructions to list
242  Settings(): what(AddressInterval::whole()) {}
243  };
244 private:
245  Settings settings_;
246  Trigger trigger_;
247 protected:
248  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
249 public:
250  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
251  static Ptr instance(const std::string &config);
252  static Ptr instance(const std::vector<std::string> &args);
253  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
254  static std::string docString();
255  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
256  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
257 };
258 
262 class CfgGraphVizDumper: public CfgAdjustmentCallback {
263 public:
264  struct Settings {
265  AddressInterval where; // what basic block(s) we should monitor (those starting within)
266  Trigger::Settings when; // once found, which event triggers the output
267  AddressInterval what; // which basic blocks should be in the output
268  bool showNeighbors; // should neighbor blocks be included in the output?
269  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
270  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
271  };
272 private:
273  Settings settings_;
274  Trigger trigger_;
275 protected:
276  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
277 public:
278  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
279  static Ptr instance(const std::string &config);
280  static Ptr instance(const std::vector<std::string> &args);
281  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
282  static std::string docString();
283  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
284  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
285 };
286 
290 class HexDumper: public CfgAdjustmentCallback {
291 public:
292  struct Settings {
293  AddressInterval where; // what basic block(s) we should monitor (those starting within)
294  Trigger::Settings when; // once found, which event triggers the output
295  AddressInterval what; // which bytes should be in the output
296  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
297  Settings(): accentSpecialValues(true) {}
298  };
299 private:
300  Settings settings_;
301  Trigger trigger_;
302 protected:
303  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
304 public:
305  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
306  static Ptr instance(const std::string &config);
307  static Ptr instance(const std::vector<std::string> &args);
308  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
309  static std::string docString();
310  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
311  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
312 };
313 
317 class Debugger: public CfgAdjustmentCallback {
318 public:
319  struct Settings {
320  AddressInterval where; // what basic block(s) should we monitor (those starting within)
321  Trigger::Settings when; // once found, which event triggers
322  };
323 private:
324  Settings settings_;
325  Trigger trigger_;
326 protected:
327  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
328 public:
329  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
330  static Ptr instance(const std::string &config);
331  static Ptr instance(const std::vector<std::string> &args);
332  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
333  static std::string docString();
334  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
335  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
336  void debug(rose_addr_t, const BasicBlock::Ptr&);
337 };
338 
344 private:
345  ThunkPredicates::Ptr predicates_;
346 protected:
347  std::vector<Function::Ptr> functions_;
348 
349 protected:
350  // use 'instance' instead
351  MatchThunk(const ThunkPredicates::Ptr &predicates)
352  : predicates_(predicates) {}
353 
354 public:
356  static Ptr instance(const ThunkPredicates::Ptr &predicates) {
357  return Ptr(new MatchThunk(predicates));
358  }
359 
363  ThunkPredicates::Ptr predicates() const { return predicates_; }
364  void predicates(const ThunkPredicates::Ptr &p) { predicates_ = p; }
367  virtual std::vector<Function::Ptr> functions() const ROSE_OVERRIDE { return functions_; }
368  virtual bool match(const Partitioner&, rose_addr_t anchor) ROSE_OVERRIDE;
369 };
370 
385 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
386  size_t leaveAtFront=16, size_t leaveAtBack=1);
387 
404 void nameStrings(const Partitioner&);
405 
412 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*);
413 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmInterpretation*);
414 size_t findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*, std::vector<Function::Ptr>&);
421 void nameConstants(const Partitioner&);
422 
426 std::vector<Function::Ptr> findNoopFunctions(const Partitioner&);
427 
434 void nameNoopFunctions(const Partitioner&);
435 
436 
438 // Partitioner conversion to AST
440 
446 
452 
459 
465 
474 SgAsmBlock* buildAst(const Partitioner&, SgAsmInterpretation *interp=NULL,
476 
485 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
486 
496 
497 } // namespace
498 
499 } // namespace
500 } // namespace
501 } // namespace
502 
503 #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:290
List some instructions at a certain time.
Definition: Modules.h:236
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:134
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:39
void nameNoopFunctions(const Partitioner &)
Give names to functions that are no-ops.
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition: Modules.h:214
Base class for matching function prologues.
Definition: Modules.h:108
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:217
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:367
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:226
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:53
A collection of related switch declarations.
Represents a synthesized function.
ThunkPredicates::Ptr predicates() const
Property: Predicates used for matching thunks.
Definition: Modules.h:363
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:335
static Ptr instance(const ThunkPredicates::Ptr &predicates)
Allocating constructor.
Definition: Modules.h:356
Results coordinated across all callbacks.
Definition: Modules.h:52
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:89
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:311
Base class for matching an instruction pattern.
Definition: Modules.h:78
static AstConstructionSettings strict()
Default strict settings.
Definition: BasicTypes.h:145
Make current instruction the final instruction of the block.
Definition: Modules.h:47
Reference-counting smart pointer.
Definition: SharedPointer.h:67
Convenient place to attach a debugger.
Definition: Modules.h:317
Trigger based on number of times called.
Definition: Utility.h:208
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:8908
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:59
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:227
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:256
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:60
Sawyer::SharedPointer< FunctionPaddingMatcher > Ptr
Shared-ownership pointer to a FunctionPaddingMatcher.
Definition: Modules.h:137
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:262
Base class for reference counted objects.
Definition: SharedObject.h:64
Make previous instruction the final instruction of the block.
Definition: Modules.h:48
void labelSymbolAddresses(Partitioner &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
Sawyer::SharedPointer< FunctionPrologueMatcher > Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition: Modules.h:111
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:81
Sawyer::SharedPointer< BasicBlockCallback > Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:42
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:284
void nameStrings(const Partitioner &)
Give labels to string constants.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:317
void predicates(const ThunkPredicates::Ptr &p)
Property: Predicates used for matching thunks.
Definition: Modules.h:364
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:61
void fixupAstCallingConventions(const Partitioner &, SgNode *ast)
Fixes calling convention results.
std::vector< Function::Ptr > findNoopFunctions(const Partitioner &)
Find functions that are no-ops.