ROSE 0.11.145.192
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
13namespace Rose {
14namespace BinaryAnalysis {
15namespace Partitioner2 {
16
73
79public:
82
93 virtual bool match(const PartitionerConstPtr&, rose_addr_t anchor) = 0;
94};
95
96
109public:
112
125 virtual std::vector<FunctionPtr> functions() const = 0;
126};
127
128
135public:
138
145 virtual rose_addr_t match(const PartitionerConstPtr&, rose_addr_t anchor) = 0;
146};
147
149// Generic modules
151
153namespace Modules {
154
163std::string canonicalFunctionName(const std::string&);
164
170
182public:
183 static Ptr instance() { return Ptr(new AddGhostSuccessors); }
184 virtual bool operator()(bool chain, const Args &args) override;
185};
186
194public:
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
208protected:
209 BasicBlockSizeLimiter(size_t maxInsns)
210 : maxInsns_(maxInsns) {}
211
212public:
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
238public:
240 typedef std::pair<rose_addr_t, rose_addr_t> AddressPair;
241
242private:
243 std::vector<AddressPair> rewrites_;
244
245protected:
246 IpRewriter(const std::vector<AddressPair> &rewrites)
247 : rewrites_(rewrites) {}
248
249public:
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
273public:
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 };
280private:
281 Settings settings_;
282 Trigger trigger_;
283protected:
284 explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
285public:
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
299public:
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 };
308private:
309 Settings settings_;
310 Trigger trigger_;
311protected:
312 CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
313public:
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
327public:
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 };
335private:
336 Settings settings_;
337 Trigger trigger_;
338protected:
339 HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
340public:
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
354public:
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 };
359private:
360 Settings settings_;
361 Trigger trigger_;
362protected:
363 Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
364public:
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
380private:
381 ThunkPredicatesPtr predicates_;
382protected:
383 std::vector<FunctionPtr> functions_;
384
385protected:
386 // use 'instance' instead
388public:
389 ~MatchThunk();
390
391public:
394
402 virtual std::vector<FunctionPtr> functions() const override;
403 virtual bool match(const PartitionerConstPtr&, rose_addr_t anchor) override;
404};
405
420AddressIntervalSet deExecuteZeros(const MemoryMapPtr &map /*in,out*/, size_t threshold,
421 size_t leaveAtFront=16, size_t leaveAtBack=1);
422
441
450size_t findSymbolFunctions(const PartitionerConstPtr&, SgAsmGenericHeader*, std::vector<FunctionPtr>&);
458
462std::vector<FunctionPtr> findNoopFunctions(const PartitionerConstPtr&);
463
471
478boost::logic::tribool isStackBasedReturn(const PartitionerConstPtr&, const BasicBlockPtr&);
479
481// Partitioner conversion to AST
483
490
496
503
509
520
530
540
541} // namespace
542
543} // namespace
544} // namespace
545} // namespace
546
547#endif
548#endif
Base class for adjusting basic blocks during discovery.
Definition Modules.h:39
virtual bool operator()(bool chain, const Args &)=0
Callback method.
BasicBlockCallbackPtr Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition Modules.h:42
Termination
Whether to terminate a basic block.
Definition Modules.h:45
@ CONTINUE_DISCOVERY
Do not explicitly terminate block here.
Definition Modules.h:46
@ TERMINATE_PRIOR
Make previous instruction the final instruction of the block.
Definition Modules.h:48
@ TERMINATE_NOW
Make current instruction the final instruction of the block.
Definition Modules.h:47
Sawyer::SharedPointer< CfgAdjustmentCallback > Ptr
Shared ownership pointer.
Base class for matching function padding.
Definition Modules.h:134
virtual rose_addr_t match(const PartitionerConstPtr &, rose_addr_t anchor)=0
Attempt to match padding.
Base class for matching function prologues.
Definition Modules.h:108
virtual std::vector< FunctionPtr > functions() const =0
Returns the function(s) for the previous successful match.
Base class for matching an instruction pattern.
Definition Modules.h:78
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition Modules.h:81
virtual bool match(const PartitionerConstPtr &, rose_addr_t anchor)=0
Attempt to match an instruction pattern.
virtual bool operator()(bool chain, const Args &args) override
Callback method.
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition Modules.h:214
size_t maxInstructions() const
Property: Maximum size of block.
Definition Modules.h:226
virtual bool operator()(bool chain, const Args &) override
Callback method.
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition Modules.h:227
Produce a GraphViz file for the CFG at a certain time.
Definition Modules.h:298
static std::string docString()
Documentation string.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:320
Convenient place to attach a debugger.
Definition Modules.h:353
static std::string docString()
Documentation string.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:371
Produce a hexdump at a certain time.
Definition Modules.h:326
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:347
static std::string docString()
Documentation string.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
List some instructions at a certain time.
Definition Modules.h:272
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:292
static std::string docString()
Documentation string.
virtual bool operator()(bool chain, const AttachedBasicBlock &args) override
Called when basic block is attached or placeholder inserted.
void rewrites(std::vector< AddressPair > &v)
Property: Pairs of old/new addresses to rewrite.
Definition Modules.h:263
virtual bool operator()(bool chain, const Args &) override
Callback method.
std::pair< rose_addr_t, rose_addr_t > AddressPair
Pairs of old and new addresses.
Definition Modules.h:240
Sawyer::SharedPointer< IpRewriter > Ptr
Shared-ownership pointer to a IpRewriter.
Definition Modules.h:251
std::vector< AddressPair > & rewrites()
Property: Pairs of old/new addresses to rewrite.
Definition Modules.h:262
const std::vector< AddressPair > & rewrites() const
Property: Pairs of old/new addresses to rewrite.
Definition Modules.h:261
static Ptr instance(const std::vector< AddressPair > &rewrites)
Constructor.
Definition Modules.h:254
static Ptr instance(const ThunkPredicatesPtr &)
Allocating constructor.
virtual bool match(const PartitionerConstPtr &, rose_addr_t anchor) override
Attempt to match an instruction pattern.
ThunkPredicatesPtr predicates() const
Property: Predicates used for matching thunks.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
void predicates(const ThunkPredicatesPtr &)
Property: Predicates used for matching thunks.
virtual bool operator()(bool chain, const Args &args) override
Callback method.
A collection of related switch declarations.
static Interval whole()
Construct an interval that covers the entire domain.
Definition Interval.h:191
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
Instruction basic block.
Represents a synthesized function.
Base class for container file headers.
Represents an interpretation of a binary container.
This class represents the base class for all IR nodes within Sage III.
void nameStrings(const PartitionerConstPtr &, const AddressInterval &)
Give labels to string constants.
void labelSymbolAddresses(const PartitionerPtr &, SgAsmGenericHeader *)
Give labels to addresses that are symbols.
void fixupAstCallingConventions(const PartitionerConstPtr &, SgNode *ast)
Fixes calling convention results.
void nameConstants(const PartitionerConstPtr &, const AddressInterval &)
Gives names to constants in instructions.
SgAsmBlock * buildBasicBlockAst(const PartitionerConstPtr &, const BasicBlockPtr &, const FunctionPtr &, const AstConstructionSettings &)
Build AST for basic block.
SgAsmBlock * buildGlobalBlockAst(const PartitionerConstPtr &, const AstConstructionSettings &)
Builds the global block AST.
std::vector< FunctionPtr > findSymbolFunctions(const PartitionerConstPtr &, SgAsmGenericHeader *)
Finds functions for which symbols exist.
std::vector< FunctionPtr > findNoopFunctions(const PartitionerConstPtr &)
Find functions that are no-ops.
AddressIntervalSet deExecuteZeros(const MemoryMapPtr &map, size_t threshold, size_t leaveAtFront=16, size_t leaveAtBack=1)
Remove execute permissions for zeros.
void demangleFunctionNames(const PartitionerConstPtr &)
Demangle all function names.
std::string canonicalFunctionName(const std::string &)
Convert system function names to ROSE canonical form.
SgAsmFunction * buildFunctionAst(const PartitionerConstPtr &, const FunctionPtr &, const AstConstructionSettings &)
Build AST for function.
boost::logic::tribool isStackBasedReturn(const PartitionerConstPtr &, const BasicBlockPtr &)
Determine if basic block is a stack-based function return.
void fixupAstPointers(SgNode *ast, SgAsmInterpretation *interp=NULL)
Fixes pointers in the AST.
void nameNoopFunctions(const PartitionerConstPtr &)
Give names to functions that are no-ops.
SgAsmBlock * buildDataBlockAst(const PartitionerConstPtr &, const DataBlockPtr &, const AstConstructionSettings &)
Build AST for data block.
SgAsmBlock * buildAst(const PartitionerConstPtr &, SgAsmInterpretation *interp=NULL, const AstConstructionSettings &settings=AstConstructionSettings::strict())
Builds an AST from the CFG.
Sawyer::SharedPointer< FunctionPaddingMatcher > FunctionPaddingMatcherPtr
Shared ownership pointer.
Sawyer::SharedPointer< FunctionPrologueMatcher > FunctionPrologueMatcherPtr
Shared ownership pointer.
Sawyer::SharedPointer< BasicBlockCallback > BasicBlockCallbackPtr
Shared ownership pointer.
The ROSE library.
Results & results
Results to control basic block discovery.
Definition Modules.h:61
PartitionerConstPtr partitioner
Partitioner requesting basic block successors.
Definition Modules.h:59
BasicBlockPtr bblock
Basic block whose successors are to be computed.
Definition Modules.h:60
Results coordinated across all callbacks.
Definition Modules.h:52
Termination terminate
Whether block should be explicitly terminated.
Definition Modules.h:53