ROSE 0.11.145.247
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 Address 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(Address address, const std::string &name, const std::string &demangledName, const std::string &hash, size_t nInsns,
199 time_t ctime, const std::string &cversion, const Library::Ptr &library);
200
201 public:
207 static Ptr instance(Address address, const std::string &name, const std::string &demangledName, const std::string &hash,
208 size_t nInsns, const Library::Ptr &library);
209
215 static Ptr instance(Address address, const std::string &name, const std::string &demangledName, const std::string &hash,
216 size_t nInsns, time_t creationTime, const std::string &creationVersion, const Library::Ptr &library);
217
218 public:
222 Address address() const;
223
227 const std::string& name() const;
228
233 const std::string& demangledName() const;
234
236 const std::string& hash() const;
237
239 size_t nInsns() const;
240
242 Library::Ptr library() const;
243
245 time_t creationTime() const;
246
248 const std::string& creationVersion() const;
249 };
250
252 // Data members
254public:
255 static Sawyer::Message::Facility mlog; // diagnostic facility for debugger
256private:
257 Settings settings_;
258 std::string databaseUrl_; // name of database to which this object is communicating
259 Sawyer::Database::Connection db_; // the database, when connected
260
261 // Cached info from the settings_.includeFile and settings_.excludeFile
262 boost::filesystem::path cachedIncludeFile_;
263 Sawyer::Container::Set<std::string> cachedIncludeNames_;
264 boost::filesystem::path cachedExcludeFile_;
265 Sawyer::Container::Set<std::string> cachedExcludeNames_;
266
267 using LibraryCache = Sawyer::Container::Map<std::string /*hash*/, Library::Ptr>;
268 LibraryCache libraryCache_; // cache of library objects read from or written to database
269
271 // Configuration settings
273public:
277 const Settings& settings() const;
278 Settings& settings();
279 void settings(const Settings&);
287
289 // Functions for attaching to the database.
291public:
295 void connect(const std::string &databaseUrl);
296
301 void createDatabase(const std::string &databaseUrl);
302
304 // Functions that modify a database.
306public:
314 Function::Ptr insertFunction(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
315 bool insertFunction(const Function::Ptr&);
324 size_t insertLibrary(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&);
325
327 // Functions for querying a database.
329public:
333 Library::Ptr library(const std::string &hash);
334
338 std::vector<Library::Ptr> libraries();
339
347 std::vector<Function::Ptr> functions();
348 std::vector<Function::Ptr> functions(const Library::Ptr&);
352 std::vector<Function::Ptr> search(const Partitioner2::PartitionerConstPtr &partitioner, const Partitioner2::FunctionPtr&);
353
355 // Utilities
357public:
361 static void initDiagnostics();
362
366 std::string hash(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&) const;
367
369 static size_t nInsns(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
370
371private:
372 // Throws an Exception if the database driver version is not adequate.
373 void checkVersion();
374
375 // Returns the database version number if there is one.
377
378 // Create the tables needed by a new database.
379 void createTables();
380
381 // Upgrade the database to the current version
382 void upgradeDatabase();
383
384 // Create or update the properties of a library in the database.
385 void createLibrary(const Library::Ptr&);
386
387 // Create or update the properties of a function in the database. It is not possible to update the address or library
388 // to which a function belongs with this call since those properties are what identifies the function -- attempting to
389 // change them simply creates a new function record without deleting the old one.
390 void createFunction(const Function::Ptr&);
391
392 // Whether the current settings specifically include this function from consideration. Exactly one of f1 or f2 should be
393 // non-null.
394 bool isIncluded(const Function::Ptr&);
395 bool isIncluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
396
397 // Whether the current settings exclude a function from consideration. Exactly one of f1 or f2 should be non-null.
398 bool isExcluded(const Function::Ptr&);
399 bool isExcluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
400
401 // Whether to consider a function for insertion into a database or as a possible match of a database function to a
402 // non-database function.
403 bool isConsidered(const Function::Ptr&);
404 bool isConsidered(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
405
406 // Load function names from files if necessary.
407 void cacheFiles();
408 void cacheNamesFromFile(const boost::filesystem::path &fileName, boost::filesystem::path &cachedFileName /*in,out*/,
409 Sawyer::Container::Set<std::string> &cachedNames /*out*/);
410
411 // Return functions based on query.
412 std::vector<Function::Ptr> functions(Sawyer::Database::Statement);
413};
414
415} // namespace
416} // namespace
417
418#endif
419#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.
std::uint64_t Address
Address.
Definition Address.h:11
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
Definition sageBuilder.C:58
std::string demangledName(std::string)
Compute demangled version of mangled name.
The ROSE library.
Settings settings
Command-line settings for the rosebud tool.