ROSE  0.9.10.103
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 
152 namespace Modules {
153 
162 std::string canonicalFunctionName(const std::string&);
163 
169 
181 public:
182  static Ptr instance() { return Ptr(new AddGhostSuccessors); }
183  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
184 };
185 
193 public:
194  static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
195  virtual bool operator()(bool chain, const Args &args) ROSE_OVERRIDE;
196 };
197 
205  size_t maxInsns_;
206 
207 protected:
208  BasicBlockSizeLimiter(size_t maxInsns)
209  : maxInsns_(maxInsns) {}
210 
211 public:
214 
216  static Ptr instance(size_t maxInsns) {
217  return Ptr(new BasicBlockSizeLimiter(maxInsns));
218  }
219 
225  size_t maxInstructions() const { return maxInsns_; }
226  void maxInstructions(size_t maxInsns) { maxInsns_ = maxInsns; }
229  virtual bool operator()(bool chain, const Args&) ROSE_OVERRIDE;
230 };
231 
236 public:
237  struct Settings {
238  AddressInterval where; // which basic block(s) we should we monitor
239  Trigger::Settings when; // once found, how often we produce a list
240  AddressInterval what; // what instructions to list
241  Settings(): what(AddressInterval::whole()) {}
242  };
243 private:
244  Settings settings_;
245  Trigger trigger_;
246 protected:
247  explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
248 public:
249  static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
250  static Ptr instance(const std::string &config);
251  static Ptr instance(const std::vector<std::string> &args);
252  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
253  static std::string docString();
254  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
255  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
256 };
257 
261 class CfgGraphVizDumper: public CfgAdjustmentCallback {
262 public:
263  struct Settings {
264  AddressInterval where; // what basic block(s) we should monitor (those starting within)
265  Trigger::Settings when; // once found, which event triggers the output
266  AddressInterval what; // which basic blocks should be in the output
267  bool showNeighbors; // should neighbor blocks be included in the output?
268  std::string fileName; // name of output; '%' gets expanded to a distinct identifier
269  Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
270  };
271 private:
272  Settings settings_;
273  Trigger trigger_;
274 protected:
275  CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
276 public:
277  static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
278  static Ptr instance(const std::string &config);
279  static Ptr instance(const std::vector<std::string> &args);
280  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
281  static std::string docString();
282  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
283  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
284 };
285 
289 class HexDumper: public CfgAdjustmentCallback {
290 public:
291  struct Settings {
292  AddressInterval where; // what basic block(s) we should monitor (those starting within)
293  Trigger::Settings when; // once found, which event triggers the output
294  AddressInterval what; // which bytes should be in the output
295  bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
296  Settings(): accentSpecialValues(true) {}
297  };
298 private:
299  Settings settings_;
300  Trigger trigger_;
301 protected:
302  HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
303 public:
304  static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
305  static Ptr instance(const std::string &config);
306  static Ptr instance(const std::vector<std::string> &args);
307  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
308  static std::string docString();
309  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
310  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
311 };
312 
316 class Debugger: public CfgAdjustmentCallback {
317 public:
318  struct Settings {
319  AddressInterval where; // what basic block(s) should we monitor (those starting within)
320  Trigger::Settings when; // once found, which event triggers
321  };
322 private:
323  Settings settings_;
324  Trigger trigger_;
325 protected:
326  Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
327 public:
328  static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
329  static Ptr instance(const std::string &config);
330  static Ptr instance(const std::vector<std::string> &args);
331  static Sawyer::CommandLine::SwitchGroup switches(Settings&);
332  static std::string docString();
333  virtual bool operator()(bool chain, const AttachedBasicBlock &args) ROSE_OVERRIDE;
334  virtual bool operator()(bool chain, const DetachedBasicBlock&) ROSE_OVERRIDE { return chain; }
335  void debug(rose_addr_t, const BasicBlock::Ptr&);
336 };
337 
352 AddressIntervalSet deExecuteZeros(const MemoryMap::Ptr &map /*in,out*/, size_t threshold,
353  size_t leaveAtFront=16, size_t leaveAtBack=1);
354 
371 void nameStrings(const Partitioner&);
372 
379 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*);
380 std::vector<Function::Ptr> findSymbolFunctions(const Partitioner&, SgAsmInterpretation*);
381 size_t findSymbolFunctions(const Partitioner&, SgAsmGenericHeader*, std::vector<Function::Ptr>&);
388 void nameConstants(const Partitioner&);
389 
393 std::vector<Function::Ptr> findNoopFunctions(const Partitioner&);
394 
401 void nameNoopFunctions(const Partitioner&);
402 
403 
405 // Partitioner conversion to AST
407 
413 
419 
426 
432 
441 SgAsmBlock* buildAst(const Partitioner&, SgAsmInterpretation *interp=NULL,
443 
452 void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL);
453 
463 
464 } // namespace
465 
466 } // namespace
467 } // namespace
468 } // namespace
469 
470 #endif
Termination
Whether to terminate a basic block.
Definition: Modules.h:44
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
Produce a hexdump at a certain time.
Definition: Modules.h:289
List some instructions at a certain time.
Definition: Modules.h:235
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:133
Instruction basic block.
Base class for CFG-adjustment callbacks.
Base class for adjusting basic blocks during discovery.
Definition: Modules.h:38
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:213
Base class for matching function prologues.
Definition: Modules.h:107
static Ptr instance(size_t maxInsns)
Constructor.
Definition: Modules.h:216
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.
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:225
Termination terminate
Whether block should be explicitly terminated.
Definition: Modules.h:52
A collection of related switch declarations.
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:334
Results coordinated across all callbacks.
Definition: Modules.h:51
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:81
virtual bool operator()(bool chain, const DetachedBasicBlock &) ROSE_OVERRIDE
Called when basic block is detached or placeholder erased.
Definition: Modules.h:310
Base class for matching an instruction pattern.
Definition: Modules.h:77
static AstConstructionSettings strict()
Default strict settings.
Definition: BasicTypes.h:137
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:316
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:8752
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:226
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:255
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: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:261
Base class for reference counted objects.
Definition: SharedObject.h:22
Make previous instruction the final instruction of the block.
Definition: Modules.h:47
void labelSymbolAddresses(Partitioner &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
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:283
void nameStrings(const Partitioner &)
Give labels to string constants.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:293
Represents an interpretation of a binary container.
Results & results
Results to control basic block discovery.
Definition: Modules.h:60
void fixupAstCallingConventions(const Partitioner &, SgNode *ast)
Fixes calling convention results.
std::vector< Function::Ptr > findNoopFunctions(const Partitioner &)
Find functions that are no-ops.