ROSE 2.15.0
Loading...
Searching...
No Matches
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#include <Rose/BinaryAnalysis/Partitioner2/ModulesJvm.h>
9
10#include <Sawyer/ProgressBar.h>
11
12#include <memory>
13#include <utility>
14
15namespace Rose {
16namespace BinaryAnalysis {
17namespace Partitioner2 {
18
26class EngineJvm: public Engine {
28 // Types
30public:
33
34 using Super = Engine;
35
37 // Data members
39private:
40 struct ArchiveProgress {
41 enum class Kind { JAR, WAR };
42
43 Kind kind = Kind::JAR;
44 ModulesJvm::Zipper *parentWar = nullptr;
45 size_t nClassFiles = 0;
46 size_t nClassesHandled = 0;
47 size_t nJarFiles = 0;
48 size_t nJarsHandled = 0;
49 std::vector<ModulesJvm::Zipper*> childJars;
50 std::unique_ptr<Sawyer::ProgressBar<size_t>> classProgressBar;
51 std::unique_ptr<Sawyer::ProgressBar<size_t>> jarProgressBar;
52 };
53
54 // Mapping of function names to virtual address
55 std::map<std::string, Address> functions_;
56
57 // Mapping of unresolved (added to partitioner) function names to virtual address
58 std::map<std::string, Address> unresolvedFunctions_;
59
60 // Listing of open jar files (maybe should be Zippers with
61 std::vector<ModulesJvm::Zipper*> jars_; // Zipper owns SgAsmGenericFile*, ie, Zipper{gf}, yeah, will have buffer
62 // Zipper.find(className)
63
64 // Multi-release JAR handling currently chooses the highest META-INF/versions/N class present in an archive instead of
65 // selecting for a configured Java runtime version. This differs from JVM semantics, but keeps eagerly loaded specimens
66 // self-consistent until runtime-version selection is exposed.
67 std::map<ModulesJvm::Zipper*, std::map<std::string, std::string>> archivePreferredClassFiles_;
68
69 // Progress totals for loaded zip containers.
70 std::map<ModulesJvm::Zipper*, ArchiveProgress> archiveProgress_;
71
72 static constexpr Address vaDefaultIncrement{4*1024};
73
74 Address nextFunctionVa_;
75
77 // Constructors
79public:
81 EngineJvm() = delete;
82 EngineJvm(const EngineJvm&) = delete;
83 EngineJvm& operator=(const EngineJvm&) = delete;
84
85protected:
86 explicit EngineJvm(const Settings&);
87
88public:
89 ~EngineJvm();
90
92 static Ptr instance();
93
95 static Ptr instance(const Settings&);
96
98 static Ptr factory();
99
101 // Overrides documented in the base class
103public:
105 bool matchFactory(const Sawyer::CommandLine::ParserResult &result, const std::vector<std::string> &specimen) const override;
106
108
109 std::list<Sawyer::CommandLine::SwitchGroup> commandLineSwitches() override;
110 std::pair<std::string, std::string> specimenNameDocumentation() override;
111
112 static Ptr promote(const Engine::Ptr&);
113
115 // The very top-level use case
117public:
142 virtual SgAsmBlock* frontend(const std::vector<std::string> &args,
143 const std::string &purpose, const std::string &description) override;
147 // Basic top-level steps
149
151 // Command-line parsing
153public:
154 virtual void checkSettings() override;
155
160
162 // Container parsing
164public:
184 virtual SgAsmInterpretation* parseContainers(const std::vector<std::string> &fileNames) override;
185
190 virtual bool isNonContainer(const std::string&) override;
191
197 virtual bool areContainersParsed() const override;
198
200 // Load specimens
201 //
202 // top-level: loadSpecimens
204public:
205
209 virtual bool areSpecimensLoaded() const override;
210
211 virtual MemoryMapPtr loadSpecimens(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
212
220 bool loadWarFile(const std::string &);
221
229 bool loadJarFile(const std::string &);
230
236 bool loadJarFile(const std::string &, ModulesJvm::Zipper* zip);
237
244
251
260
269
276 boost::filesystem::path pathToClass(const std::string &fqcn);
277
284 void discoverFunctionCalls(SgAsmJvmMethod*, SgAsmJvmConstantPool*, std::map<std::string,Address> &, std::set<std::string> &);
285
286private:
287 void registerWarProgress(ModulesJvm::Zipper*);
288 void registerJarProgress(ModulesJvm::Zipper*, ModulesJvm::Zipper *parentWar = nullptr);
289 const std::map<std::string, std::string>& preferredArchiveClassFiles(ModulesJvm::Zipper*);
290 Address loadArchiveClassFiles(ModulesJvm::Zipper*, SgAsmGenericFileList*, Address);
291 void beginArchiveClassProgress(ModulesJvm::Zipper*);
292 void beginWarJarProgress(ModulesJvm::Zipper*);
293 void noteWarJarHandled(ModulesJvm::Zipper*);
294 void noteArchiveClassHandled(ModulesJvm::Zipper*);
295 void updateLoaderProgress(size_t current, size_t total) const;
296 void createClassProgressBar(ArchiveProgress&, const std::string&) const;
297 void createJarProgressBar(ArchiveProgress&, const std::string&) const;
298
299public:
319 virtual PartitionerPtr partition(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
320
322 // Disassembler
324public:
325
327 // Partitioner high-level functions
328 //
329 // top-level: partition
331public:
332
334 virtual void checkCreatePartitionerPrerequisites() const override;
335
341
343 virtual void runPartitionerInit(const PartitionerPtr&) override;
344 virtual void runPartitionerRecursive(const PartitionerPtr&) override;
345 virtual void runPartitionerFinal(const PartitionerPtr&) override;
346
347
349 // Partitioner mid-level functions
350 //
351 // These are the functions called by the partitioner high-level stuff. These are sometimes overridden in subclasses,
352 // although it is more likely that the high-level stuff is overridden.
354public:
355
357 // Partitioner low-level functions
358 //
359 // These are functions that a subclass seldom overrides, and maybe even shouldn't override because of their complexity or
360 // the way the interact with one another.
362public:
363
365 // Build AST
367public:
382 virtual SgAsmBlock* buildAst(const std::vector<std::string> &fileNames = std::vector<std::string>()) override;
386 // Settings and properties
388public:
394 const std::vector<std::string>& classPath() const;
395
402 const std::string& isaName() const /*final*/;
403 virtual void isaName(const std::string&);
412 const std::vector<Address>& functionStartingVas() const /*final*/;
413 std::vector<Address>& functionStartingVas() /*final*/;
417 SgProject* roseFrontendReplacement(const std::vector<std::string> &fileNames);
418
419 Architecture::BaseConstPtr obtainArchitecture() override;
420
422 // Internal stuff
424protected:
425 // Similar to ::frontend but a lot less complicated.
426 virtual SgProject* roseFrontendReplacement(const std::vector<boost::filesystem::path> &fileNames) override;
427};
428
430// JVM Module
432
434namespace ModulesJvm {
435
439bool isJavaClassFile(const boost::filesystem::path&);
440
444bool isJavaJarFile(const boost::filesystem::path&);
445
449bool isJavaWarFile(const boost::filesystem::path&);
450
467boost::filesystem::path pathToClassFile(const std::string &fqcn, const std::vector<std::string> &cp);
468
481bool present(const std::string &fqcn, const std::string &path);
482
483} // namespace ModulesJvm
484
485} // namespace
486} // namespace
487} // namespace
488
489#endif
490#endif
Engine for Java Virtual Machine (JVM) specimens.
Definition EngineJvm.h:26
virtual MemoryMapPtr loadSpecimens(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Parse specimen binary containers.
EngineJvm()=delete
Default constructor.
static Ptr factory()
Allocate a factory.
virtual void checkSettings() override
Check settings after command-line is processed.
std::pair< std::string, std::string > specimenNameDocumentation() override
Documentation about how the specimen is specified.
virtual void runPartitionerInit(const PartitionerPtr &) override
Parse specimen binary containers.
virtual void runPartitionerFinal(const PartitionerPtr &) override
Parse specimen binary containers.
const std::vector< Address > & functionStartingVas() const
Property: Starting addresses for disassembly.
virtual SgAsmBlock * frontend(const std::vector< std::string > &args, const std::string &purpose, const std::string &description) override
Most basic usage of the partitioner.
virtual SgAsmBlock * buildAst(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Obtain an abstract syntax tree.
void discoverFunctionCalls(SgAsmJvmMethod *, SgAsmJvmConstantPool *, std::map< std::string, Address > &, std::set< std::string > &)
Discover function calls (invoke instructions) made from a method.
virtual SgAsmInterpretation * parseContainers(const std::vector< std::string > &fileNames) override
Parse specimen binary containers.
bool loadJarFile(const std::string &, ModulesJvm::Zipper *zip)
Load a jar file from a zipped container by opening its contents.
const std::vector< std::string > & classPath() const
Setting: JVM class path.
bool loadWarFile(const std::string &)
Load a war file by opening its contents.
virtual bool isNonContainer(const std::string &) override
Determine whether a specimen name is a non-container.
virtual bool areSpecimensLoaded() const override
Returns true if specimens are loaded.
PartitionerPtr createJvmTunedPartitioner()
Create a partitioner for JVM.
static Ptr instance(const Settings &)
Allocating constructor with settings.
std::list< Sawyer::CommandLine::SwitchGroup > commandLineSwitches() override
Command-line switches for a particular engine.
virtual PartitionerPtr partition(const std::vector< std::string > &fileNames=std::vector< std::string >()) override
Partition instructions into basic blocks and functions.
const std::string & isaName() const
Property: Instruction set architecture name.
Address loadDiscoverableClasses(SgAsmGenericFileList *, Address)
Load classes discoverable from the file list starting at the given address.
EnginePtr instanceFromFactory(const Settings &) override
Virtual constructor for factories.
Address loadClassFile(boost::filesystem::path, SgAsmGenericFileList *, Address)
Load a class file by parsing its contents at the given address.
bool matchFactory(const Sawyer::CommandLine::ParserResult &result, const std::vector< std::string > &specimen) const override
Predicate for matching a concrete engine factory by command-line parser result and specimen.
Address loadBaseClassAndInterfaces(const std::string &, SgAsmGenericFileList *, Address)
Recursively find and load all super classes starting at the given address.
static Sawyer::CommandLine::SwitchGroup jvmSwitches(JvmSettings &)
Command-line switches related to the JVM engine behavior.
Address loadClass(uint16_t, SgAsmJvmConstantPool *, SgAsmGenericFileList *, Address)
Load class and super class starting at the given address.
SgProject * roseFrontendReplacement(const std::vector< std::string > &fileNames)
Replacement for frontend for Jvm files only.
virtual void checkCreatePartitionerPrerequisites() const override
Check that we have everything necessary to create a partitioner.
bool loadJarFile(const std::string &)
Load a jar file by opening its contents.
Architecture::BaseConstPtr obtainArchitecture() override
Determine the architecture.
virtual PartitionerPtr createPartitioner() override
Parse specimen binary containers.
boost::filesystem::path pathToClass(const std::string &fqcn)
Path to the given fully qualified class name (FQCN).
virtual bool areContainersParsed() const override
Returns true if containers are parsed.
static Ptr instance()
Allocating constructor.
virtual void runPartitionerRecursive(const PartitionerPtr &) override
Parse specimen binary containers.
Base class for engines driving the partitioner.
const std::vector< std::string > & specimen() const
Property: specimen.
Engine()=delete
Default constructor.
The result from parsing a command line.
A collection of related switch declarations.
Reference-counting intrusive smart pointer.
Instruction basic block.
List of AST file node pointers.
Represents an interpretation of a binary container.
Represents an JVM constant pool.
This class represents a source project, with a list of SgFile objects and global information about th...
bool isJavaWarFile(const boost::filesystem::path &)
True if named file exists and is a Java war file.
bool isJavaJarFile(const boost::filesystem::path &)
True if named file exists and is a Java jar file.
bool isJavaClassFile(const boost::filesystem::path &)
True if named file exists and is a Java class file.
boost::filesystem::path pathToClassFile(const std::string &fqcn, const std::vector< std::string > &cp)
Searchs the classpath setting for the path to a fully qualified class name.
bool present(const std::string &fqcn, const std::string &path)
Searchs the path for the presence of a fully qualified class name.
Sawyer::SharedPointer< EngineJvm > EngineJvmPtr
Shared-ownership pointer for EngineJvm.
std::uint64_t Address
Address.
Definition Address.h:11
The ROSE library.