ROSE 0.11.145.147
LibraryIdentification.h
1#ifndef ROSE_BinaryAnalysis_LibraryIdentification_H
2#define ROSE_BinaryAnalysis_LibraryIdentification_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_LIBRARY_IDENTIFICATION
5
6#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
7
8#include <Sawyer/CommandLine.h>
9#include <Sawyer/Database.h>
10#include <Sawyer/Message.h>
11#include <Sawyer/SharedObject.h>
12#include <Sawyer/SharedPointer.h>
13
14#include <ctime>
15#include <regex>
16#include <string>
17#include <vector>
18
19namespace Rose {
20namespace BinaryAnalysis {
21
33class LibraryIdentification {
35 // Settings
37public:
39 struct Settings {
45
51 std::vector<std::regex> includeRes;
52
59 boost::filesystem::path includeFile;
60
66
72 std::vector<std::regex> excludeRes;
73
80 boost::filesystem::path excludeFile;
81
86 size_t minFunctionInsns = 0;
87
97 bool identifyFunctionsByHash = false;
98 };
99
101 // Libraries
103public:
105 class Library: public Sawyer::SharedObject {
106 public:
109
110 private:
111 std::string hash_; // required unique key for library
112 std::string name_; // required name of library
113 std::string version_; // optional version of library
114 std::string architecture_; // optional architecture string
115 time_t ctime_ = 0; // Unix time at which this object was created; zero if unknown
116 std::string cversion_; // ROSE version which created this object; empty if unknown
117
118 protected:
119 Library(const std::string &hash, const std::string &name, const std::string &version, const std::string &architecture,
120 time_t ctime, const std::string &cversion);
121
122 public:
128 static Ptr instance(const std::string &hash, const std::string &name, const std::string &version,
129 const std::string &architecture);
130
136 static Ptr instance(const std::string &hash, const std::string &name, const std::string &version,
137 const std::string &architecture, time_t creationTime, const std::string &creationVersion);
138
139 public:
144 const std::string& hash() const;
145
150 const std::string& name() const;
151
155 const std::string& version() const;
156
160 const std::string& architecture() const;
161
166 time_t creationTime() const;
167
172 const std::string& creationVersion() const;
173 };
174
176 // Functions
178public:
182 class Function: public Sawyer::SharedObject {
183 public:
186
187 private:
188 rose_addr_t address_; // required starting address that identifies function in library
189 std::string name_; // optional name of function (possibly mangled)
190 std::string demangledName_; // optional demangled name of function
191 std::string hash_; // hash used for matching
192 size_t nInsns_; // number of instructions in function
193 Library::Ptr library_; // library to which function belongs
194 time_t ctime_ = 0; // time at which this object was created; zero if unknown
195 std::string cversion_; // ROSE version that created this object; empty if unknown
196
197 protected:
198 Function(rose_addr_t address, const std::string &name, const std::string &demangledName, const std::string &hash,
199 size_t nInsns, time_t ctime, const std::string &cversion, const Library::Ptr &library);
200
201 public:
207 static Ptr instance(rose_addr_t address, const std::string &name, const std::string &demangledName,
208 const std::string &hash, size_t nInsns, const Library::Ptr &library);
209
215 static Ptr instance(rose_addr_t address, const std::string &name, const std::string &demangledName,
216 const std::string &hash, size_t nInsns, time_t creationTime, const std::string &creationVersion,
217 const Library::Ptr &library);
218
219 public:
223 rose_addr_t address() const;
224
228 const std::string& name() const;
229
234 const std::string& demangledName() const;
235
237 const std::string& hash() const;
238
240 size_t nInsns() const;
241
243 Library::Ptr library() const;
244
246 time_t creationTime() const;
247
249 const std::string& creationVersion() const;
250 };
251
253 // Data members
255public:
256 static Sawyer::Message::Facility mlog; // diagnostic facility for debugger
257private:
258 Settings settings_;
259 std::string databaseUrl_; // name of database to which this object is communicating
260 Sawyer::Database::Connection db_; // the database, when connected
261
262 // Cached info from the settings_.includeFile and settings_.excludeFile
263 boost::filesystem::path cachedIncludeFile_;
264 Sawyer::Container::Set<std::string> cachedIncludeNames_;
265 boost::filesystem::path cachedExcludeFile_;
266 Sawyer::Container::Set<std::string> cachedExcludeNames_;
267
268 using LibraryCache = Sawyer::Container::Map<std::string /*hash*/, Library::Ptr>;
269 LibraryCache libraryCache_; // cache of library objects read from or written to database
270
272 // Configuration settings
274public:
278 const Settings& settings() const;
279 Settings& settings();
280 void settings(const Settings&);
288
290 // Functions for attaching to the database.
292public:
296 void connect(const std::string &databaseUrl);
297
302 void createDatabase(const std::string &databaseUrl);
303
305 // Functions that modify a database.
307public:
315 Function::Ptr insertFunction(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
316 bool insertFunction(const Function::Ptr&);
325 size_t insertLibrary(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&);
326
328 // Functions for querying a database.
330public:
334 Library::Ptr library(const std::string &hash);
335
339 std::vector<Library::Ptr> libraries();
340
348 std::vector<Function::Ptr> functions();
349 std::vector<Function::Ptr> functions(const Library::Ptr&);
353 std::vector<Function::Ptr> search(const Partitioner2::PartitionerConstPtr &partitioner, const Partitioner2::FunctionPtr&);
354
356 // Utilities
358public:
362 static void initDiagnostics();
363
367 std::string hash(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&) const;
368
370 static size_t nInsns(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
371
372private:
373 // Throws an Exception if the database driver version is not adequate.
374 void checkVersion();
375
376 // Returns the database version number if there is one.
378
379 // Create the tables needed by a new database.
380 void createTables();
381
382 // Upgrade the database to the current version
383 void upgradeDatabase();
384
385 // Create or update the properties of a library in the database.
386 void createLibrary(const Library::Ptr&);
387
388 // Create or update the properties of a function in the database. It is not possible to update the address or library
389 // to which a function belongs with this call since those properties are what identifies the function -- attempting to
390 // change them simply creates a new function record without deleting the old one.
391 void createFunction(const Function::Ptr&);
392
393 // Whether the current settings specifically include this function from consideration. Exactly one of f1 or f2 should be
394 // non-null.
395 bool isIncluded(const Function::Ptr&);
396 bool isIncluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
397
398 // Whether the current settings exclude a function from consideration. Exactly one of f1 or f2 should be non-null.
399 bool isExcluded(const Function::Ptr&);
400 bool isExcluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
401
402 // Whether to consider a function for insertion into a database or as a possible match of a database function to a
403 // non-database function.
404 bool isConsidered(const Function::Ptr&);
405 bool isConsidered(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
406
407 // Load function names from files if necessary.
408 void cacheFiles();
409 void cacheNamesFromFile(const boost::filesystem::path &fileName, boost::filesystem::path &cachedFileName /*in,out*/,
410 Sawyer::Container::Set<std::string> &cachedNames /*out*/);
411
412 // Return functions based on query.
413 std::vector<Function::Ptr> functions(Sawyer::Database::Statement);
414};
415
416} // namespace
417} // namespace
418
419#endif
420#endif
A collection of related switch declarations.
Container associating values with keys.
Definition Sawyer/Map.h:72
Ordered set of values.
Definition Set.h:56
Collection of streams.
Definition Message.h:1606
Holds a value or nothing.
Definition Optional.h:56
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
void initDiagnostics()
Initialize diagnostics.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
Hash hash(const std::vector< Ptr > &)
Hash zero or more expressions.
Sawyer::CommandLine::SwitchGroup commandLineSwitches(Settings &settings)
Command-line switches for unparser settings.
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
std::string demangledName(std::string)
Compute demangled version of mangled name.
The ROSE library.
Settings settings
Command-line settings for the rosebud tool.