ROSE 2.20.0
Loading...
Searching...
No Matches
BinaryAnalysis/ByteCode/Analysis.h
1#ifndef ROSE_BinaryAnalysis_ByteCode_Analysis_H
2#define ROSE_BinaryAnalysis_ByteCode_Analysis_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
7#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
8#include <Rose/Progress.h>
9
11
12namespace Rose {
13namespace BinaryAnalysis {
14namespace ByteCode {
15
16namespace P2 = Partitioner2;
17
18using BasicBlockPtr = P2::BasicBlockPtr;
19using PartitionerPtr = P2::PartitionerPtr;
20using InstructionMap = std::map<Address, SgAsmInstruction*>;
21
22// Forward references
23class Class;
24class Namespace;
25class Container;
26
27using ClassPtr = Sawyer::SharedPointer<Class>;
28using NamespacePtr = Sawyer::SharedPointer<Namespace>;
29using ContainerPtr = Sawyer::SharedPointer<Container>;
30
36 public Sawyer::SharedFromThis<Field> {
37 public:
40
41 virtual ~Field();
42
43public:
44 virtual std::string name() const = 0;
45
46protected:
47 Field();
48};
49
56 public Sawyer::SharedFromThis<Method> {
57 public:
60
61 virtual ~Method();
62
63 public:
64 const std::string& name() const;
65 Address address() const;
66
68 void finalize();
69
70 virtual bool returnsVoid() const;
71 virtual bool isStatic() const = 0;
72 virtual bool isSystemReserved(const std::string &name) const = 0;
73
74 virtual const SgAsmInstructionList* instructions() const = 0;
75 virtual void decode(const Disassembler::BasePtr&) const = 0;
76
77 virtual std::string descriptor() const = 0;
78
79 /* The method identity; class + name + descriptor */
80 virtual std::string identity() const;
81
82 /* Annotate the AST (.e.g., add comments to instructions) */
83 virtual void annotate() = 0;
84
85 /* Set of instruction branch targets */
86 std::set<Address> targets() const;
87
88 /* Accessors to the declaring class for this method */
89 Class* declaringClass() const;
90 void declaringClass(Class *declaringClass);
91
92 /* Retrieve the instruction at the given address */
93 SgAsmInstruction* instructionAt(Address va) const;
94
95 // Methods associated with basic blocks (Rose::BinaryAnalysis::Partitioner2)
96 //
97 const std::vector<BasicBlockPtr>& blocks() const;
98 void append(BasicBlockPtr bb);
99
100 Method() = delete;
101
102 protected:
103 Method(std::string name, Address va);
104
105 private:
106 std::string name_;
107
108 protected:
109 Address address_ = 0;
110
111 private:
112 Class* class_ = nullptr; // non-owning pointer to the declaring class
113 P2::FunctionPtr function_;
114 std::vector<BasicBlockPtr> blocks_;
115 InstructionMap instructionMap_;
116};
117
123 public Sawyer::SharedFromThis<Interface> {
124 public:
127
128 virtual ~Interface();
129
130 virtual std::string name() const = 0;
131
132 protected:
133 Interface();
134};
135
141 public Sawyer::SharedFromThis<Attribute> {
142public:
145
146 virtual ~Attribute();
147
148 virtual std::string name() const = 0;
149
150 protected:
151 Attribute();
152};
153
159 public Sawyer::SharedFromThis<Class> {
160 public:
163 using NamespacePtr = Sawyer::SharedPointer<Namespace>; // forward declaration
164
165 virtual ~Class();
166
168 // Dynamic pointer cast. No-op since this is the base class
169 // static Class promote(const Class&);
170
171 const std::string& name() const;
172 const std::string& baseClassName() const;
173
174 Address address() const;
175 NamespacePtr nameSpace() const;
176
177 ByteCode::Method::Ptr findMethod(const std::string &name, const std::string &descriptor) const;
178
179 virtual std::string qualifiedName() const;
180 virtual std::string typeSeparator() const = 0;
181 virtual const std::vector<std::string>& strings();
182
183 const std::vector<Field::Ptr>& fields() const;
184 const std::vector<Method::Ptr>& methods() const;
185 const std::vector<Attribute::Ptr>& attributes() const;
186 const std::vector<Interface::Ptr>& interfaces() const;
187
189 void finalize();
190
191 virtual void partition(const PartitionerPtr &partitioner, std::map<std::string,Address> &discoveredFunctions,
192 const Progress::Ptr &progress = Progress::Ptr());
193 virtual void digraph() const;
194 virtual void dump() = 0;
195
196 Class() = delete;
197
198 private:
199 std::string name_;
200 Address address_ = 0; // Base virtual address assigned to this loaded class file
201 NamespacePtr namespace_;
202
203 protected:
204 Class(std::string name, Address va, NamespacePtr ns);
205
206 std::string baseClassName_;
207
208 std::vector<Field::Ptr> fields_;
209 std::vector<Method::Ptr> methods_;
210 std::vector<Attribute::Ptr> attributes_;
211 std::vector<Interface::Ptr> interfaces_;
212 std::vector<std::string> strings_;
213};
214
220 public Sawyer::SharedFromThis<Namespace> {
221 public:
224 using ContainerPtr = Sawyer::SharedPointer<Container>; // forward declaration
225
226 virtual ~Namespace() = default;
227
229 static Ptr instance(std::string name, ContainerPtr container);
230
231 virtual std::string name() const;
232 virtual void partition(const PartitionerPtr &partitioner,
233 std::map<std::string,Address> &discoveredFunctions);
234
235 void append(Class::Ptr ptr);
236
237 const std::vector<ByteCode::Class::Ptr>& classes() const;
238
239 protected:
240 Namespace() = delete;
241 explicit Namespace(std::string name, ContainerPtr container);
242
243 std::vector<ByteCode::Class::Ptr> classes_;
244
245 private:
246 std::string name_;
247};
248
254 public Sawyer::SharedFromThis<Container> {
255 public:
258
259 virtual ~Container() = default;
260
261 /* A unique (per container) virtual address for system/library functions */
262 static Address nextSystemReservedVa();
263
264 public:
265 virtual std::string name() const;
266 virtual bool isSystemReserved(const std::string &name) const = 0;
267 virtual void partition(const PartitionerPtr &partitioner);
268
269 void append(Namespace::Ptr ptr);
270
271 const std::vector<Namespace::Ptr>& namespaces() const;
272
273 protected:
274 Container() = delete;
275 explicit Container(std::string name);
276
277 std::vector<Namespace::Ptr> namespaces_;
278
279 private:
280 std::string name_;
281 static Address nextSystemReservedVa_;
282};
283
289 public Sawyer::SharedFromThis<ClassRepository> {
290 public:
293
294 ClassRepository() = default;
295 virtual ~ClassRepository() = default;
296
297 public:
298
300 static Ptr instance();
301
306 bool insert(const Class::Ptr &cls);
307
309 bool contains(const std::string &name) const;
310
312 ByteCode::ClassPtr findClass(const std::string &name) const;
313
315 bool isAssignableTo(const ClassPtr &source, const ClassPtr &target) const;
316
317 private:
318 std::map<std::string, ClassPtr> classes_;
319};
320
321} // namespace
322} // namespace
323} // namespace
324
325#endif
326#endif
bool isAssignableTo(const ClassPtr &source, const ClassPtr &target) const
Returns true if a value of the source class can be assigned to the target.
static Ptr instance()
Allocating constructor.
ByteCode::ClassPtr findClass(const std::string &name) const
Returns the Class with the given name, null if not present.
bool insert(const Class::Ptr &cls)
Inserts a class into the repository.
bool contains(const std::string &name) const
Returns true if a class with the given name is present.
void finalize()
Complete initialization of the class once partitioning is done.
void finalize()
Complete initialization of the method once decoding is done.
static Ptr instance(std::string name, ContainerPtr container)
Allocating constructor.
Creates SharedPointer from this.
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
List of SgAsmInstruction nodes.
Base class for machine instructions.
Sawyer::SharedPointer< Partitioner > PartitionerPtr
Shared-ownership pointer for Partitioner.
Sawyer::SharedPointer< BasicBlock > BasicBlockPtr
Shared-ownersip pointer for BasicBlock.
Map< Address, SgAsmInstruction * > InstructionMap
Mapping from instruction addresses to instructions.
std::uint64_t Address
Address.
Definition Address.h:11
The ROSE library.