ROSE 0.11.145.147
EngineBinary.h
1#ifndef ROSE_BinaryAnalysis_Partitioner2_EngineBinary_H
2#define ROSE_BinaryAnalysis_Partitioner2_EngineBinary_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/Architecture/BasicTypes.h>
7#include <Rose/BinaryAnalysis/Partitioner2/Engine.h>
8#include <Rose/BinaryAnalysis/Partitioner2/ModulesLinux.h>
9#include <Rose/BinaryAnalysis/Partitioner2/Utility.h>
10#include <Rose/BinaryAnalysis/SerialIo.h>
11
12#include <boost/noncopyable.hpp>
13#include <boost/regex.hpp>
14#include <stdexcept>
15
16#ifdef ROSE_ENABLE_PYTHON_API
17#undef slots // stupid Qt pollution
18#include <boost/python.hpp>
19#endif
20
21namespace Rose {
22namespace BinaryAnalysis {
23namespace Partitioner2 {
24
48class EngineBinary: public Engine {
50 // Types
52public:
55
56private:
57 using Super = Engine;
58
60 // Data members
62private:
63 BinaryLoaderPtr binaryLoader_; // how to remap, link, and fixup
64 ModulesLinux::LibcStartMain::Ptr libcStartMain_; // looking for "main" by analyzing libc_start_main?
65 ThunkPredicatesPtr functionMatcherThunks_; // predicates to find thunks when looking for functions
66 ThunkPredicatesPtr functionSplittingThunks_; // predicates for splitting thunks from front of functions
67
69 // Constructors
71protected:
73 EngineBinary() = delete;
74
75 explicit EngineBinary(const Settings&);
76
77public:
78 virtual ~EngineBinary();
79
81 static Ptr instance();
82
84 static Ptr instance(const Settings&);
85
87 static Ptr factory();
88
90 // Command-line parsing
92public:
93
98
103
108
113
118
119
121 // Container parsing
122 //
123 // top-level: parseContainers
125public:
130 virtual void loadVxCore(const std::string &spec);
131
133 // Load specimens
134 //
135 // top-level: loadSpecimens
137public:
164 virtual void loadContainers(const std::vector<std::string> &fileNames);
165
170 virtual void loadNonContainers(const std::vector<std::string> &names);
171
173 // Partitioner high-level functions
174 //
175 // top-level: partition
177public:
183
189
196
202
204 // Partitioner mid-level functions
205 //
206 // These are the functions called by the partitioner high-level stuff. These are sometimes overridden in subclasses,
207 // although it is more likely that the high-level stuff is overridden.
209public:
216 virtual std::vector<FunctionPtr> makeEntryFunctions(const PartitionerPtr&, SgAsmInterpretation*);
217
224 virtual std::vector<FunctionPtr> makeErrorHandlingFunctions(const PartitionerPtr&, SgAsmInterpretation*);
225
235 virtual std::vector<FunctionPtr> makeImportFunctions(const PartitionerPtr&, SgAsmInterpretation*);
236
243 virtual std::vector<FunctionPtr> makeExportFunctions(const PartitionerPtr&, SgAsmInterpretation*);
244
251 virtual std::vector<FunctionPtr> makeSymbolFunctions(const PartitionerPtr&, SgAsmInterpretation*);
252
260 virtual std::vector<FunctionPtr> makeContainerFunctions(const PartitionerPtr&, SgAsmInterpretation*);
261
268 virtual std::vector<FunctionPtr> makeInterruptVectorFunctions(const PartitionerPtr&, const AddressInterval &vector);
269
274 virtual std::vector<FunctionPtr> makeUserFunctions(const PartitionerPtr&, const std::vector<rose_addr_t>&);
275
283
295 virtual FunctionPtr makeNextDataReferencedFunction(const PartitionerConstPtr&, rose_addr_t &startVa /*in,out*/);
296
311
318 virtual std::vector<FunctionPtr> makeCalledFunctions(const PartitionerPtr&);
319
335 virtual std::vector<FunctionPtr> makeNextPrologueFunction(const PartitionerPtr&, rose_addr_t startVa);
336 virtual std::vector<FunctionPtr> makeNextPrologueFunction(const PartitionerPtr&, rose_addr_t startVa,
337 rose_addr_t &lastSearchedVa);
358 virtual std::vector<FunctionPtr>
359 makeFunctionFromInterFunctionCalls(const PartitionerPtr&, rose_addr_t &startVa /*in,out*/);
360
367 virtual void discoverFunctions(const PartitionerPtr&);
368
383 virtual std::set<rose_addr_t> attachDeadCodeToFunction(const PartitionerPtr&, const FunctionPtr&,
384 size_t maxIterations=size_t(-1));
385
394
399 virtual std::vector<DataBlockPtr> attachPaddingToFunctions(const PartitionerPtr&);
400
413
422
428
435 virtual std::set<rose_addr_t> attachDeadCodeToFunctions(const PartitionerPtr&, size_t maxIterations=size_t(-1));
436
446 virtual std::vector<DataBlockPtr> attachSurroundedDataToFunctions(const PartitionerPtr&);
447
449 // Partitioner low-level functions
450 //
451 // These are functions that a subclass seldom overrides, and maybe even shouldn't override because of their complexity or
452 // the way the interact with one another.
454public:
465 virtual bool makeNextCallReturnEdge(const PartitionerPtr&, boost::logic::tribool assumeCallReturns);
466
473
489
490
492 // Build AST
494
496 // Settings and properties
498public:
505 BinaryLoaderPtr binaryLoader() const /*final*/;
506 virtual void binaryLoader(const BinaryLoaderPtr&);
534 // Python API support functions
536#ifdef ROSE_ENABLE_PYTHON_API
537
538 // Similar to frontend, but returns a partitioner rather than an AST since the Python API doesn't yet support ASTs.
539 PartitionerPtr pythonParseVector(boost::python::list &pyArgs, const std::string &purpose, const std::string &description);
540 PartitionerPtr pythonParseSingle(const std::string &specimen, const std::string &purpose, const std::string &description);
541
542#endif
543
545 // Internal stuff
547private:
548 void init();
549
551 // Overrides documented in the base class
553public:
554 virtual bool matchFactory(const std::vector<std::string> &specimen) const override;
555 virtual EnginePtr instanceFromFactory(const Settings&) override;
556 virtual void reset() override;
557
558 using Engine::frontend;
559 SgAsmBlock* frontend(const std::vector<std::string> &args,
560 const std::string &purpose, const std::string &description) override;
561
563 virtual SgAsmInterpretation* parseContainers(const std::vector<std::string> &fileNames) override;
564
566 virtual MemoryMapPtr loadSpecimens(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
567
568 using Engine::partition;
569 virtual PartitionerPtr partition(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
570
571 using Engine::buildAst;
572 virtual SgAsmBlock* buildAst(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
573
574 virtual std::list<Sawyer::CommandLine::SwitchGroup> commandLineSwitches() override;
575 virtual std::pair<std::string, std::string> specimenNameDocumentation() override;
576 virtual bool isNonContainer(const std::string&) override;
577 virtual bool areContainersParsed() const override;
579 virtual void runPartitionerInit(const PartitionerPtr&) override;
580 virtual void runPartitionerRecursive(const PartitionerPtr&) override;
581 virtual void runPartitionerFinal(const PartitionerPtr&) override;
582 virtual SgProject* roseFrontendReplacement(const std::vector<boost::filesystem::path> &fileNames) override;
583};
584
585} // namespace
586} // namespace
587} // namespace
588
589#endif
590#endif
Engine for specimens containing machine instructions.
virtual std::vector< FunctionPtr > makeCalledFunctions(const PartitionerPtr &)
Make functions for function call edges.
virtual std::set< rose_addr_t > attachDeadCodeToFunction(const PartitionerPtr &, const FunctionPtr &, size_t maxIterations=size_t(-1))
Attach dead code to function.
virtual std::vector< FunctionPtr > makeExportFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions at export addresses.
virtual BasicBlockPtr makeNextBasicBlock(const PartitionerPtr &)
Discover a basic block.
virtual std::vector< DataBlockPtr > attachPaddingToFunctions(const PartitionerPtr &)
Attach padding to all functions.
static Sawyer::CommandLine::SwitchGroup disassemblerSwitches(DisassemblerSettings &)
Command-line switches related to decoding instructions.
virtual EnginePtr instanceFromFactory(const Settings &) override
Virtual constructor for factories.
virtual void runPartitionerInit(const PartitionerPtr &) override
Finds interesting things to work on initially.
virtual std::set< rose_addr_t > attachDeadCodeToFunctions(const PartitionerPtr &, size_t maxIterations=size_t(-1))
Attach dead code to functions.
virtual BinaryLoaderPtr obtainLoader(const BinaryLoaderPtr &hint)
Obtain a binary loader.
virtual std::list< Sawyer::CommandLine::SwitchGroup > commandLineSwitches() override
Command-line switches for a particular engine.
virtual bool partitionCilSections(const PartitionerPtr &)
Partition any sections containing CIL code.
virtual SgAsmBlock * buildAst(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Obtain an abstract syntax tree.
virtual std::vector< FunctionPtr > makeImportFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions at import trampolines.
virtual std::vector< FunctionPtr > makeUserFunctions(const PartitionerPtr &, const std::vector< rose_addr_t > &)
Make a function at each specified address.
virtual std::vector< DataBlockPtr > attachSurroundedDataToFunctions(const PartitionerPtr &)
Attach intra-function data to functions.
virtual void reset() override
Reset the engine to its initial state.
static Sawyer::CommandLine::SwitchGroup engineSwitches(EngineSettings &)
Command-line switches related to the general engine behavior.
virtual BasicBlockPtr makeNextBasicBlockFromPlaceholder(const PartitionerPtr &)
Discover basic block at next placeholder.
virtual void runPartitionerFinal(const PartitionerPtr &) override
Runs the final parts of partitioning.
virtual std::vector< FunctionPtr > makeContainerFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions based on specimen container.
virtual void attachBlocksToFunctions(const PartitionerPtr &)
Attach basic blocks to functions.
virtual PartitionerPtr createPartitionerFromAst(SgAsmInterpretation *)
Create a partitioner from an AST.
virtual std::vector< FunctionPtr > makeNextPrologueFunction(const PartitionerPtr &, rose_addr_t startVa)
Make function at prologue pattern.
virtual FunctionPtr makeNextCodeReferencedFunction(const PartitionerConstPtr &)
Scan instruction ASTs to function pointers.
virtual void discoverFunctions(const PartitionerPtr &)
Discover as many functions as possible.
ThunkPredicatesPtr functionMatcherThunks() const
Property: Predicate for finding functions that are thunks.
virtual SgAsmInterpretation * parseContainers(const std::vector< std::string > &fileNames) override
Parse specimen binary containers.
virtual std::vector< FunctionPtr > makeSymbolFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions for symbols.
virtual bool isNonContainer(const std::string &) override
Determine whether a specimen name is a non-container.
SgAsmBlock * frontend(const std::vector< std::string > &args, const std::string &purpose, const std::string &description) override
Most basic usage of the partitioner.
virtual size_t attachSurroundedCodeToFunctions(const PartitionerPtr &)
Attach intra-function basic blocks to functions.
virtual std::vector< FunctionPtr > makeEntryFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions at specimen entry addresses.
virtual std::vector< FunctionPtr > makeErrorHandlingFunctions(const PartitionerPtr &, SgAsmInterpretation *)
Make functions at error handling addresses.
static Sawyer::CommandLine::SwitchGroup astConstructionSwitches(AstConstructionSettings &)
Command-line switches related to constructing an AST from the partitioner.
virtual std::vector< FunctionPtr > makeFunctionFromInterFunctionCalls(const PartitionerPtr &, rose_addr_t &startVa)
Make functions from inter-function calls.
virtual void runPartitionerRecursive(const PartitionerPtr &) override
Runs the recursive part of partioning.
virtual DataBlockPtr attachPaddingToFunction(const PartitionerPtr &, const FunctionPtr &)
Attach function padding to function.
virtual PartitionerPtr partition(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Partition instructions into basic blocks and functions.
virtual FunctionPtr makeNextDataReferencedFunction(const PartitionerConstPtr &, rose_addr_t &startVa)
Scan read-only data to find function pointers.
static Sawyer::CommandLine::SwitchGroup partitionerSwitches(PartitionerSettings &)
Command-line switches related to partitioning instructions.
virtual std::vector< FunctionPtr > makeNextPrologueFunction(const PartitionerPtr &, rose_addr_t startVa, rose_addr_t &lastSearchedVa)
Make function at prologue pattern.
BinaryLoaderPtr binaryLoader() const
Property: binary loader.
static Ptr factory()
Allocate a factory.
virtual bool makeNextCallReturnEdge(const PartitionerPtr &, boost::logic::tribool assumeCallReturns)
Insert a call-return edge and discover its basic block.
virtual MemoryMapPtr loadSpecimens(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Load and/or link interpretation.
static Sawyer::CommandLine::SwitchGroup loaderSwitches(LoaderSettings &)
Command-line switches related to loading specimen into memory.
virtual BinaryLoaderPtr obtainLoader()
Obtain a binary loader.
virtual bool areContainersParsed() const override
Returns true if containers are parsed.
bool hasCilCodeSection()
Determine whether the interpretation header contains a CIL code section.
virtual bool matchFactory(const std::vector< std::string > &specimen) const override
Predicate for matching a concrete engine factory by settings and specimen.
virtual void loadNonContainers(const std::vector< std::string > &names)
Loads memory from non-containers.
virtual PartitionerPtr createTunedPartitioner()
Create a tuned partitioner.
virtual PartitionerPtr createPartitioner() override
Create partitioner.
static Ptr instance(const Settings &)
Allocating constructor with settings.
ThunkPredicatesPtr functionSplittingThunks() const
Property: Predicate for finding thunks at the start of functions.
virtual std::pair< std::string, std::string > specimenNameDocumentation() override
Documentation about how the specimen is specified.
static Ptr instance()
Allocating constructor.
virtual std::vector< FunctionPtr > makeInterruptVectorFunctions(const PartitionerPtr &, const AddressInterval &vector)
Make functions from an interrupt vector.
virtual void loadVxCore(const std::string &spec)
Parses a vxcore specification and initializes memory.
virtual size_t attachAllSurroundedCodeToFunctions(const PartitionerPtr &)
Attach all possible intra-function basic blocks to functions.
virtual void loadContainers(const std::vector< std::string > &fileNames)
Loads memory from binary containers.
virtual void discoverBasicBlocks(const PartitionerPtr &)
Discover as many basic blocks as possible.
Base class for engines driving the partitioner.
const std::vector< std::string > & specimen() const
Property: specimen.
SgAsmBlock * frontend(int argc, char *argv[], const std::string &purpose, const std::string &description)
Most basic usage of the partitioner.
Engine()=delete
Default constructor.
virtual SgAsmBlock * buildAst(const std::vector< std::string > &fileNames=std::vector< std::string >())=0
Obtain an abstract syntax tree.
virtual SgAsmInterpretation * parseContainers(const std::vector< std::string > &fileNames)=0
Parse specimen binary containers.
virtual MemoryMapPtr loadSpecimens(const std::vector< std::string > &fileNames=std::vector< std::string >())=0
Load and/or link interpretation.
virtual PartitionerPtr partition(const std::vector< std::string > &fileNames=std::vector< std::string >())=0
Partition instructions into basic blocks and functions.
A collection of related switch declarations.
Reference-counting intrusive smart pointer.
Instruction basic block.
Represents an interpretation of a binary container.
This class represents a source project, with a list of SgFile objects and global information about th...
Sawyer::SharedPointer< EngineBinary > EngineBinaryPtr
Shared-ownership pointer for EngineBinary.
The ROSE library.