ROSE 2.1.0
Loading...
Searching...
No Matches
FlatBuffers.h
1#ifndef ROSE_BinaryAnalysis_Serialization_FlatBuffers_H
2#define ROSE_BinaryAnalysis_Serialization_FlatBuffers_H
3
4#include <featureTests.h>
5#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#ifdef ROSE_ENABLE_FLATBUFFERS
7
8#include <Rose/BinaryAnalysis/Architecture/BasicTypes.h>
9#include <Rose/BinaryAnalysis/MemoryMap.h>
10#include <Rose/BinaryAnalysis/Partitioner2/BasicBlock.h>
11#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
12#include <Rose/BinaryAnalysis/Partitioner2/Partitioner.h>
13
14#include <rosePublicConfig.h>
15
16#include <boost/filesystem/path.hpp>
17
18#include <cstddef>
19#include <cstdint>
20#include <iosfwd>
21#include <utility>
22#include <vector>
23
24#include <flatbuffers/flatbuffers.h>
25
26#include <Rose/BinaryAnalysis/Serialization/FlatBufferSchema.h>
27
28namespace Rose {
29namespace BinaryAnalysis {
30namespace Serialization {
31
33
34namespace FlatBuffers {
35
36static constexpr uint32_t version = 1;
37
58class Serializer {
59 public:
60 explicit Serializer(const P2::PartitionerConstPtr&);
61 ~Serializer() = default;
62
63 Serializer(const Serializer&) = delete;
64 Serializer& operator=(const Serializer&) = delete;
65
66 template <typename T> using Handle = flatbuffers::Offset<T>;
67
71 void save();
72
76 std::pair<const uint8_t*, size_t> buffer() const;
77
79 void write(std::ostream&) const;
80
82 void write(const boost::filesystem::path&) const;
83
84 private:
85 P2::PartitionerConstPtr partitioner_;
86 std::vector<char> bytes_;
87 std::unique_ptr<flatbuffers::FlatBufferBuilder> builder_;
88
89 protected:
90 Handle<Architecture> architecture(const BinaryAnalysis::Architecture::BaseConstPtr& arch);
91 Handle<Instruction> instruction(const SgAsmInstruction* const& insn);
92 Handle<BasicBlock> basicBlock(const P2::BasicBlockPtr& bb);
93 Handle<Function> function(const P2::FunctionPtr& f);
94 Handle<CfgEdge> cfgEdge(const P2::ControlFlowGraph::Edge& e);
95 Handle<Cfg> cfg(const P2::ControlFlowGraph& cfg);
96 Handle<Segment> segment(const BinaryAnalysis::MemoryMap::Super::Node& seg);
97 Handle<MemoryMap> mmap(const BinaryAnalysis::MemoryMap& map);
98 std::pair<Handle<InstructionList>, Handle<BasicBlockList>>
99 instructionsBasicBlocks(const std::vector<P2::BasicBlockPtr>& bbs);
100 Handle<FunctionList> functions(const std::vector<P2::FunctionPtr>& funs);
101 Handle<Root> partitioner(/*partitioner_*/);
102};
103
128class Deserializer {
129 public:
130 Deserializer() = default;
131
133 static Deserializer fromFile(const boost::filesystem::path&);
134
136 static Deserializer fromStream(std::istream&);
137
139 static Deserializer fromBytes(std::vector<char>&&);
140
142 static Deserializer fromBytes(const std::vector<char>&);
143
145 bool verify() const;
146
152 void preferStoredBytes(bool prefer) { preferStoredBytes_ = prefer; }
153
164 P2::PartitionerPtr load(const P2::BasePartitionerSettings& settings);
165 // Load with default settings (useful in testing)
166 P2::PartitionerPtr load();
167
168 private:
169 // Underlying bytes
170 std::vector<char> bytes_;
171
172 // Current partitioner
173 P2::PartitionerPtr partitioner_;
174
175 // Index ROSE instructions and basic blocks by start address.
176 // This is needed because generally FlatBuffer structures use addresses as lightweight references.
177 // For example, FlatBuffer basic blocks save their constituent instructions as a list of addresses.
178 // We use external explicit maps (instead of the partitioner) so that the Deserializer can create detached
179 // partitioner objects.
180 std::unordered_map<Address, SgAsmInstruction*> instructions_;
181 std::unordered_map<Address, P2::BasicBlock::Ptr> basic_blocks_;
182
192 bool preferStoredBytes_ = false;
193
199 BinaryAnalysis::MemoryMap::Ptr mmap(const MemoryMap* map) const;
200
213 SgAsmInstruction* disassembleFromBytes(Address addr, const flatbuffers::Vector<uint8_t>* bytes) const;
214
223 // The instruction method makes no assumptions other than that the input pointer is non-null.
224 void instruction(const Instruction* const& instr);
225 // The basicBlock method assumes that all instructions have been discovered and that instructions_ is up-to-date.
226 void basicBlock(const BasicBlock* const& bb);
227 // The function method assumes that all basic blocks have been discovered and that basic_blocks_ is up-to-date.
228 void function(const Function* const& fun);
229 // The cfg method assumes that all functions and basic blocks have been discovered and that basic_blocks_ is
230 // up-to-date.
231 void cfg(const Cfg* const& cfg);
232};
233
234} // namespace FlatBuffers
235} // namespace Serialization
236} // namespace BinaryAnalysis
237} // namespace Rose
238
239#endif
240#endif
241#endif
Base class for machine instructions.
Binary function detection.
ROSE_DLL_API void load(SgProject *project, std::list< std::string > const &filepaths)
Load ASTs that have been saved to files.
The ROSE library.