ROSE  0.9.11.155
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 <boost/logic/tribool.hpp>
12 #include <Sawyer/SharedPointer.h>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace Partitioner2 {
17 
41 public:
44 
46  enum Termination {
50  };
51 
53  struct Results {
55  Results(): terminate(CONTINUE_DISCOVERY) {}
56  };
57 
59  struct Args {
63  Args(const Partitioner &partitioner, const BasicBlock::Ptr &bblock, Results &results)
64  : partitioner(partitioner), bblock(bblock), results(results) {}
65  };
66 
72  virtual bool operator()(bool chain, const Args&) = 0;
73 };
74 
80 public:
83 
94  virtual bool match(const Partitioner&, rose_addr_t anchor) = 0;
95 };
96 
97 
110 public:
113 
126  virtual std::vector<Function::Ptr> functions() const = 0;
127 };
128 
129 
136 public:
139 
146  virtual rose_addr_t match(const Partitioner&, rose_addr_t anchor) = 0;
147 };
148 
150 // Generic modules
152 
154 namespace Modules {
155 
164 std::string canonicalFunctionName(const std::string&);
165 
171 
183 public:
184  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
185  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
186 };
187 
195 public:
196  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
197  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
198 };
199 
207  size_t maxInsns_;
208 
209 protected:
210  BasicBlockSizeLimiter(size_t maxInsns)
211  : maxInsns_(maxInsns) {}
212 
213 public:
216 
218  static Ptr instance(size_t maxInsns) {
219  return Ptr(new BasicBlockSizeLimiter(maxInsns));
220  }
221 
227  size_t maxInstructions() const { return maxInsns_; }
228  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
231  virtual bool operator()(bool chain, const Args&) ROSE_OVERRIDE;
232 };
233 
238 public:
239  struct Settings {
240  AddressInterval where; // which basic block(s) we should we monitor
241  Trigger::Settings when; // once found, how often we produce a list
242  AddressInterval what; // what instructions to list
243  Settings(): what(AddressInterval::whole()) {}
244  };
245 private:
246  Settings settings_;
247  Trigger trigger_;
248 protected:
249  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
250 public:
251  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
252  static Ptr instance(const std::string &config);
253  static Ptr instance(const std::vector<std::string> &args);
254  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
255  static std::string docString();
256  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
257  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
258 };
259 
263 class CfgGraphVizDumper: public CfgAdjustmentCallback {
264 public:
265  struct Settings {
266  AddressInterval where; // what basic block(s) we should monitor (those starting within)
267  Trigger::Settings when; // once found, which event triggers the output
268  AddressInterval what; // which basic blocks should be in the output
269  bool showNeighbors; // should neighbor blocks be included in the output?
270  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
271  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
272  };
273 private:
274  Settings settings_;
275  Trigger trigger_;
276 protected:
277  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
278 public:
279  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
280  static Ptr instance(const std::string &config);
281  static Ptr instance(const std::vector<std::string> &args);
282  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
283  static std::string docString();
284  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
285  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
286 };
287 
291 class HexDumper: public CfgAdjustmentCallback {
292 public:
293  struct Settings {
294  AddressInterval where; // what basic block(s) we should monitor (those starting within)
295  Trigger::Settings when; // once found, which event triggers the output
296  AddressInterval what; // which bytes should be in the output
297  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
298  Settings(): accentSpecialValues(true) {}
299  };
300 private:
301  Settings settings_;
302  Trigger trigger_;
303 protected:
304  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
305 public:
306  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
307  static Ptr instance(const std::string &config);
308  static Ptr instance(const std::vector<std::string> &args);
309  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
310  static std::string docString();
311  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
312  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
313 };
314 
318 class Debugger: public CfgAdjustmentCallback {
319 public:
320  struct Settings {
321  AddressInterval where; // what basic block(s) should we monitor (those starting within)
322  Trigger::Settings when; // once found, which event triggers
323  };
324 private:
325  Settings settings_;
326  Trigger trigger_;
327 protected:
328  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
329 public:
330  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
331  static Ptr instance(const std::string &config);
332  static Ptr instance(const std::vector<std::string> &args);
333  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
334  static std::string docString();
335  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
336  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
337  void debug(rose_addr_t, const BasicBlock::Ptr&);
338 };
339 
345 private:
346  ThunkPredicates::Ptr predicates_;
347 protected:
348  std::vector<Function::Ptr> functions_;
349 
350 protected:
351  // use 'instance' instead
352  MatchThunk(const ThunkPredicates::Ptr &predicates)
353  : predicates_(predicates) {}
354 
355 public:
357  static Ptr instance(const ThunkPredicates::Ptr &predicates) {
358  return Ptr(new MatchThunk(predicates));
359  }
360 
364  ThunkPredicates::Ptr predicates() const { return predicates_; }
365  void predicates(const ThunkPredicates::Ptr &p) { predicates_ = p; }
368  virtual std::vector<Function::Ptr> functions() const ROSE_OVERRIDE { return functions_; }
369  virtual bool match(const Partitioner&, rose_addr_t anchor) ROSE_OVERRIDE;
370 };
371 
386 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
387  size_t leaveAtFront=16, size_t leaveAtBack=1);
388 
405 void nameStrings(const Partitioner&);
406 
413 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*);
414 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmInterpretation*);
415 size_t findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*, std::vector<Function::Ptr>&);
422 void nameConstants(const Partitioner&);
423 
427 std::vector<Function::Ptr> findNoopFunctions(const Partitioner&);
428 
435 void nameNoopFunctions(const Partitioner&);
436 
443 boost::logic::tribool isStackBasedReturn(const Partitioner&, const BasicBlock::Ptr&);
444 
446 // Partitioner conversion to AST
448 
454 
460 
467 
473 
482 SgAsmBlock* buildAst(const Partitioner&, SgAsmInterpretation *interp=NULL,
484 
493 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
494 
504 
505 } // namespace
506 
507 } // namespace
508 } // namespace
509 } // namespace
510 
511 #endif
Termination
Whether to terminate a basic block.
Definition: Modules.h:46
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
Produce a hexdump at a certain time.
Definition: Modules.h:291
List some instructions at a certain time.
Definition: Modules.h:237
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:135
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:40
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:215
Base class for matching function prologues.
Definition: Modules.h:109
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:218
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:368
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:227
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:54
A collection of related switch declarations.
Represents a synthesized function.
ThunkPredicates::Ptr predicates() const
Property: Predicates used for matching thunks.
Definition: Modules.h:364
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:336
static Ptr instance(const ThunkPredicates::Ptr &predicates)
Allocating constructor.
Definition: Modules.h:357
Results coordinated across all callbacks.
Definition: Modules.h:53
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:312
Base class for matching an instruction pattern.
Definition: Modules.h:79
static AstConstructionSettings strict()
Default strict settings.
Definition: BasicTypes.h:145
Make current instruction the final instruction of the block.
Definition: Modules.h:48
Reference-counting smart pointer.
Definition: SharedPointer.h:67
Convenient place to attach a debugger.
Definition: Modules.h:318
Trigger based on number of times called.
Definition: Utility.h:202
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:8968
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:60
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition: Modules.h:228
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:257
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:61
Sawyer::SharedPointer< FunctionPaddingMatcher > Ptr
Shared-ownership pointer to a FunctionPaddingMatcher.
Definition: Modules.h:138
Produce a GraphViz file for the CFG at a certain time.
Definition: Modules.h:263
Base class for reference counted objects.
Definition: SharedObject.h:64
Make previous instruction the final instruction of the block.
Definition: Modules.h:49
void labelSymbolAddresses(Partitioner &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
Sawyer::SharedPointer< FunctionPrologueMatcher > Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition: Modules.h:112
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:82
Sawyer::SharedPointer< BasicBlockCallback > Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition: Modules.h:43
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:285
void nameStrings(const Partitioner &)
Give labels to string constants.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:316
void predicates(const ThunkPredicates::Ptr &p)
Property: Predicates used for matching thunks.
Definition: Modules.h:365
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:62
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.