ROSE  0.11.145.0
EngineJvm.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_EngineJvm_H
2 #define ROSE_BinaryAnalysis_Partitioner2_EngineJvm_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/ByteCode/Jvm.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/Engine.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 namespace Partitioner2 {
12 
20 class EngineJvm: public Engine {
21 public:
23  using Ptr = EngineJvmPtr;
24 
25  //TODO:DELETE_ME/MOVE_ME
27  class Exception: public Rose::Exception {
28  public:
29  Exception(const std::string &mesg)
30  : Rose::Exception(mesg) {}
31  ~Exception() throw () {}
32  };
33 
35  // Internal data structures
37 private:
38 
40  // Data members
42 private:
43  // Mapping of class names to virtual address
44  std::map<std::string, SgAsmGenericFile*> classes_;
45 
46  // Mapping of function names to virtual address
47  std::map<std::string, rose_addr_t> functions_;
48 
49  // Mapping of unresolved (added to partitioner) function names to virtual address
50  std::map<std::string, rose_addr_t> unresolvedFunctions_;
51 
52  static constexpr rose_addr_t vaDefaultIncrement{4*1024};
53  rose_addr_t nextFunctionVa_;
54 
56  // Constructors
58 public:
60  EngineJvm() = delete;
61  EngineJvm(const EngineJvm&) = delete;
62  EngineJvm& operator=(const EngineJvm&) = delete;
63 
64 protected:
65  explicit EngineJvm(const Settings &settings);
66 
67 public:
68  ~EngineJvm();
69 
71  static Ptr instance(const Settings&);
72 
74  static Ptr factory();
75 
76  virtual bool matchFactory(const std::vector<std::string> &specimen) const override;
77  virtual EnginePtr instanceFromFactory(const Settings&) override;
78 
80  // The very top-level use case
82 public:
107  virtual SgAsmBlock* frontend(const std::vector<std::string> &args,
108  const std::string &purpose, const std::string &description) override;
111  // Basic top-level steps
114 public:
115 
117  // Command-line parsing
119 
120  virtual std::pair<std::string, std::string> specimenNameDocumentation() override;
121 
123  // Container parsing
125 public:
145  virtual SgAsmInterpretation* parseContainers(const std::vector<std::string> &fileNames) override;
146 
151  virtual bool isNonContainer(const std::string&) override;
152 
158  virtual bool areContainersParsed() const override;
159 
161  // Load specimens
162  //
163  // top-level: loadSpecimens
165 public:
166 
170  virtual bool areSpecimensLoaded() const override;
171 
192  virtual MemoryMapPtr loadSpecimens(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
193 
199  rose_addr_t loadClassFile(boost::filesystem::path, SgAsmGenericFileList*, rose_addr_t);
200 
206  rose_addr_t loadSuperClasses(const std::string &, SgAsmGenericFileList*, rose_addr_t);
207 
215  rose_addr_t loadDiscoverableClasses(SgAsmGenericFileList*, rose_addr_t);
216 
224  rose_addr_t loadClass(uint16_t, SgAsmJvmConstantPool*, SgAsmGenericFileList*, rose_addr_t);
225 
231  boost::filesystem::path pathToClass(const std::string &);
232 
238  void discoverFunctionCalls(SgAsmJvmMethod*, SgAsmJvmConstantPool*, std::map<std::string,rose_addr_t> &);
239 
259  virtual PartitionerPtr partition(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
260 
262  // Disassembler
264 public:
265 
267  // Partitioner high-level functions
268  //
269  // top-level: partition
271 public:
272 
274  virtual void checkCreatePartitionerPrerequisites() const override;
275 
282 
287  virtual PartitionerPtr createPartitioner() override;
288 
292  virtual void runPartitionerInit(const PartitionerPtr&) override;
293 
297  virtual void runPartitionerRecursive(const PartitionerPtr&) override;
298 
303  virtual void runPartitionerFinal(const PartitionerPtr&) override;
304 
306  // Partitioner mid-level functions
307  //
308  // These are the functions called by the partitioner high-level stuff. These are sometimes overridden in subclasses,
309  // although it is more likely that the high-level stuff is overridden.
311 public:
312 
319  virtual void discoverBasicBlocks(const PartitionerPtr&, const ByteCode::Method*);
320 
327  virtual void discoverFunctions(const PartitionerPtr&, const ByteCode::Class*);
328 
330  // Partitioner low-level functions
331  //
332  // These are functions that a subclass seldom overrides, and maybe even shouldn't override because of their complexity or
333  // the way the interact with one another.
335 public:
336 
338  // Build AST
340 public:
355  virtual SgAsmBlock* buildAst(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
358  // Settings and properties
361 public:
368  const std::string& isaName() const /*final*/;
369  virtual void isaName(const std::string&);
378  const std::vector<rose_addr_t>& functionStartingVas() const /*final*/;
379  std::vector<rose_addr_t>& functionStartingVas() /*final*/;
382  // Internal stuff
385 protected:
386  // Similar to ::frontend but a lot less complicated.
387  virtual SgProject* roseFrontendReplacement(const std::vector<boost::filesystem::path> &fileNames) override;
388 };
389 
391 // JVM Module
393 
395 namespace ModulesJvm {
396 
401 bool isJavaClassFile(const boost::filesystem::path&);
402 
403 } // namespace
404 
405 } // namespace
406 } // namespace
407 } // namespace
408 
409 #endif
410 #endif
virtual void discoverFunctions(const PartitionerPtr &, const ByteCode::Class *)
Discover as many functions as possible.
virtual void runPartitionerInit(const PartitionerPtr &) override
Finds interesting things to work on initially.
Instruction basic block.
virtual SgAsmBlock * frontend(const std::vector< std::string > &args, const std::string &purpose, const std::string &description) override
Most basic usage of the partitioner.
EngineJvmPtr Ptr
Shared ownership pointer.
Definition: EngineJvm.h:23
Base class for engines driving the partitioner.
virtual MemoryMapPtr loadSpecimens(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Load interpretation.
virtual void runPartitionerFinal(const PartitionerPtr &) override
Runs the final parts of partitioning.
virtual bool areSpecimensLoaded() const override
Returns true if specimens are loaded.
boost::filesystem::path pathToClass(const std::string &)
Path to the given class.
virtual bool areContainersParsed() const override
Returns true if containers are parsed.
Represents an JVM constant pool.
STL namespace.
static Ptr factory()
Allocate a factory.
Main namespace for the ROSE library.
virtual SgAsmInterpretation * parseContainers(const std::vector< std::string > &fileNames) override
Parse specimen binary containers.
rose_addr_t loadSuperClasses(const std::string &, SgAsmGenericFileList *, rose_addr_t)
Recursively find and load all super classes starting at the given address.
const std::vector< rose_addr_t > & functionStartingVas() const
Property: Starting addresses for disassembly.
List of AST file node pointers.
rose_addr_t loadClass(uint16_t, SgAsmJvmConstantPool *, SgAsmGenericFileList *, rose_addr_t)
Load class and super class starting at the given address.
void discoverFunctionCalls(SgAsmJvmMethod *, SgAsmJvmConstantPool *, std::map< std::string, rose_addr_t > &)
Discover function calls (invoke instructions) made from a method.
rose_addr_t loadDiscoverableClasses(SgAsmGenericFileList *, rose_addr_t)
Load classes discoverable from the file list starting at the given address.
virtual PartitionerPtr partition(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Partition instructions into basic blocks and functions.
virtual void checkCreatePartitionerPrerequisites() const override
Check that we have everything necessary to create a partitioner.
rose_addr_t loadClassFile(boost::filesystem::path, SgAsmGenericFileList *, rose_addr_t)
Load a class file by parsing its contents at the given address.
const std::string & isaName() const
Property: Instruction set architecture name.
virtual void discoverBasicBlocks(const PartitionerPtr &, const ByteCode::Method *)
Discover as many basic blocks as possible.
virtual bool isNonContainer(const std::string &) override
Determine whether a specimen name is a non-container.
EngineJvm()=delete
Default constructor.
Sawyer::SharedPointer< EngineJvm > EngineJvmPtr
Shared-ownership pointer for EngineJvm.
static Ptr instance(const Settings &)
Allocating constructor with settings.
This class represents a source project, with a list of SgFile objects and global information about th...
virtual SgAsmBlock * buildAst(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Obtain an abstract syntax tree.
PartitionerPtr createJvmTunedPartitioner()
Create a partitioner for JVM.
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9
bool isJavaClassFile(const boost::filesystem::path &)
True if named file is a Java class file.
virtual void runPartitionerRecursive(const PartitionerPtr &) override
Runs the recursive part of partioning.
Represents an interpretation of a binary container.
Engine for Java Virtual Machine (JVM) specimens.
Definition: EngineJvm.h:20
virtual PartitionerPtr createPartitioner() override
Create partitioner.