ROSE 0.11.145.250
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&, Address anchor) = 0;
94};
95
96
109public:
112
125 virtual std::vector<FunctionPtr> functions() const = 0;
126};
127
128
135public:
138
144 virtual Address match(const PartitionerConstPtr&, Address anchor) = 0;
145};
146
148// Generic modules
150
152namespace Modules {
153
162std::string canonicalFunctionName(const std::string&);
163
169
181public:
182 static Ptr instance() { return Ptr(new AddGhostSuccessors); }
183 virtual bool operator()(bool chain, const Args &args) override;
184};
185
193public:
194 static Ptr instance() { return Ptr(new PreventDiscontiguousBlocks); }
195 virtual bool operator()(bool chain, const Args &args) override;
196};
197
205 size_t maxInsns_;
206
207protected:
208 BasicBlockSizeLimiter(size_t maxInsns)
209 : maxInsns_(maxInsns) {}
210
211public:
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&) override;
230};
231
237public:
239 typedef std::pair<Address, Address> AddressPair;
240
241private:
242 std::vector<AddressPair> rewrites_;
243
244protected:
245 IpRewriter(const std::vector<AddressPair> &rewrites)
246 : rewrites_(rewrites) {}
247
248public:
251
253 static Ptr instance(const std::vector<AddressPair> &rewrites) {
254 return Ptr(new IpRewriter(rewrites));
255 }
256
260 const std::vector<AddressPair>& rewrites() const { return rewrites_; }
261 std::vector<AddressPair>& rewrites() { return rewrites_; }
262 void rewrites(std::vector<AddressPair> &v) { rewrites_ = v; }
265 virtual bool operator()(bool chain, const Args&) override;
266};
267
272public:
273 struct Settings {
274 AddressInterval where; // which basic block(s) we should we monitor
275 Trigger::Settings when; // once found, how often we produce a list
276 AddressInterval what; // what instructions to list
277 Settings(): what(AddressInterval::whole()) {}
278 };
279private:
280 Settings settings_;
281 Trigger trigger_;
282protected:
283 explicit InstructionLister(const Settings &settings): settings_(settings), trigger_(settings.when) {}
284public:
285 static Ptr instance(const Settings &settings) { return Ptr(new InstructionLister(settings)); }
286 static Ptr instance(const std::string &config);
287 static Ptr instance(const std::vector<std::string> &args);
288 static Sawyer::CommandLine::SwitchGroup switches(Settings&);
289 static std::string docString();
290 virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
291 virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
292};
293
298public:
299 struct Settings {
300 AddressInterval where; // what basic block(s) we should monitor (those starting within)
301 Trigger::Settings when; // once found, which event triggers the output
302 AddressInterval what; // which basic blocks should be in the output
303 bool showNeighbors; // should neighbor blocks be included in the output?
304 std::string fileName; // name of output; '%' gets expanded to a distinct identifier
305 Settings(): what(AddressInterval::whole()), showNeighbors(true), fileName("cfg-%.dot") {}
306 };
307private:
308 Settings settings_;
309 Trigger trigger_;
310protected:
311 CfgGraphVizDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
312public:
313 static Ptr instance(const Settings &settings) { return Ptr(new CfgGraphVizDumper(settings)); }
314 static Ptr instance(const std::string &config);
315 static Ptr instance(const std::vector<std::string> &args);
316 static Sawyer::CommandLine::SwitchGroup switches(Settings&);
317 static std::string docString();
318 virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
319 virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
320};
321
326public:
327 struct Settings {
328 AddressInterval where; // what basic block(s) we should monitor (those starting within)
329 Trigger::Settings when; // once found, which event triggers the output
330 AddressInterval what; // which bytes should be in the output
331 bool accentSpecialValues; // use "." and "##" for 0 and 0xff?
332 Settings(): accentSpecialValues(true) {}
333 };
334private:
335 Settings settings_;
336 Trigger trigger_;
337protected:
338 HexDumper(const Settings &settings): settings_(settings), trigger_(settings.when) {}
339public:
340 static Ptr instance(const Settings &settings) { return Ptr(new HexDumper(settings)); }
341 static Ptr instance(const std::string &config);
342 static Ptr instance(const std::vector<std::string> &args);
343 static Sawyer::CommandLine::SwitchGroup switches(Settings&);
344 static std::string docString();
345 virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
346 virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
347};
348
353public:
354 struct Settings {
355 AddressInterval where; // what basic block(s) should we monitor (those starting within)
356 Trigger::Settings when; // once found, which event triggers
357 };
358private:
359 Settings settings_;
360 Trigger trigger_;
361protected:
362 Debugger(const Settings &settings): settings_(settings), trigger_(settings.when) {}
363public:
364 static Ptr instance(const Settings &settings) { return Ptr(new Debugger(settings)); }
365 static Ptr instance(const std::string &config);
366 static Ptr instance(const std::vector<std::string> &args);
367 static Sawyer::CommandLine::SwitchGroup switches(Settings&);
368 static std::string docString();
369 virtual bool operator()(bool chain, const AttachedBasicBlock &args) override;
370 virtual bool operator()(bool chain, const DetachedBasicBlock&) override { return chain; }
371 void debug(Address, const BasicBlockPtr&);
372};
373
379private:
380 ThunkPredicatesPtr predicates_;
381protected:
382 std::vector<FunctionPtr> functions_;
383
384protected:
385 // use 'instance' instead
387public:
388 ~MatchThunk();
389
390public:
393
401 virtual std::vector<FunctionPtr> functions() const override;
402 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
403};
404
419AddressIntervalSet deExecuteZeros(const MemoryMapPtr &map /*in,out*/, size_t threshold,
420 size_t leaveAtFront=16, size_t leaveAtBack=1);
421
440
449size_t findSymbolFunctions(const PartitionerConstPtr&, SgAsmGenericHeader*, std::vector<FunctionPtr>&);
457
461std::vector<FunctionPtr> findNoopFunctions(const PartitionerConstPtr&);
462
470
477boost::logic::tribool isStackBasedReturn(const PartitionerConstPtr&, const BasicBlockPtr&);
478
480// Partitioner conversion to AST
482
489
495
502
508
519
529
539
540} // namespace
541
542} // namespace
543} // namespace
544} // namespace
545
546#endif
547#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 Address match(const PartitionerConstPtr &, Address 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
virtual bool match(const PartitionerConstPtr &, Address anchor)=0
Attempt to match an instruction pattern.
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition Modules.h:81
virtual bool operator()(bool chain, const Args &args) override
Callback method.
Sawyer::SharedPointer< BasicBlockSizeLimiter > Ptr
Shared-ownership pointer to a BasicBlockSizeLimiter.
Definition Modules.h:213
size_t maxInstructions() const
Property: Maximum size of block.
Definition Modules.h:225
virtual bool operator()(bool chain, const Args &) override
Callback method.
void maxInstructions(size_t maxInsns)
Property: Maximum size of block.
Definition Modules.h:226
Produce a GraphViz file for the CFG at a certain time.
Definition Modules.h:297
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:319
Convenient place to attach a debugger.
Definition Modules.h:352
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:370
Produce a hexdump at a certain time.
Definition Modules.h:325
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:346
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:271
virtual bool operator()(bool chain, const DetachedBasicBlock &) override
Called when basic block is detached or placeholder erased.
Definition Modules.h:291
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:262
virtual bool operator()(bool chain, const Args &) override
Callback method.
std::pair< Address, Address > AddressPair
Pairs of old and new addresses.
Definition Modules.h:239
Sawyer::SharedPointer< IpRewriter > Ptr
Shared-ownership pointer to a IpRewriter.
Definition Modules.h:250
std::vector< AddressPair > & rewrites()
Property: Pairs of old/new addresses to rewrite.
Definition Modules.h:261
const std::vector< AddressPair > & rewrites() const
Property: Pairs of old/new addresses to rewrite.
Definition Modules.h:260
static Ptr instance(const std::vector< AddressPair > &rewrites)
Constructor.
Definition Modules.h:253
static Ptr instance(const ThunkPredicatesPtr &)
Allocating constructor.
virtual bool match(const PartitionerConstPtr &, Address 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.
std::uint64_t Address
Address.
Definition Address.h:11
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