ROSE  0.11.145.0
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/Database.h>
9 #include <Sawyer/SharedObject.h>
10 #include <Sawyer/SharedPointer.h>
11 #include <ctime>
12 #include <regex>
13 #include <string>
14 #include <vector>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 
30 class LibraryIdentification {
32  // Settings
34 public:
36  struct Settings {
42 
48  std::vector<std::regex> includeRes;
49 
56  boost::filesystem::path includeFile;
57 
63 
69  std::vector<std::regex> excludeRes;
70 
77  boost::filesystem::path excludeFile;
78 
83  size_t minFunctionInsns = 0;
84 
91  bool identifyFunctionsByHash = false;
92  };
93 
95  // Libraries
97 public:
99  class Library: public Sawyer::SharedObject {
100  public:
103 
104  private:
105  std::string hash_; // required unique key for library
106  std::string name_; // required name of library
107  std::string version_; // optional version of library
108  std::string architecture_; // optional architecture string
109  time_t ctime_ = 0; // Unix time at which this object was created; zero if unknown
110  std::string cversion_; // ROSE version which created this object; empty if unknown
111 
112  protected:
113  Library(const std::string &hash, const std::string &name, const std::string &version, const std::string &architecture,
114  time_t ctime, const std::string &cversion);
115 
116  public:
122  static Ptr instance(const std::string &hash, const std::string &name, const std::string &version,
123  const std::string &architecture);
124 
130  static Ptr instance(const std::string &hash, const std::string &name, const std::string &version,
131  const std::string &architecture, time_t creationTime, const std::string &creationVersion);
132 
133  public:
138  const std::string& hash() const;
139 
144  const std::string& name() const;
145 
149  const std::string& version() const;
150 
154  const std::string& architecture() const;
155 
160  time_t creationTime() const;
161 
166  const std::string& creationVersion() const;
167  };
168 
170  // Functions
172 public:
176  class Function: public Sawyer::SharedObject {
177  public:
180 
181  private:
182  rose_addr_t address_; // required starting address that identifies function in library
183  std::string name_; // optional name of function (possibly mangled)
184  std::string demangledName_; // optional demangled name of function
185  std::string hash_; // hash used for matching
186  size_t nInsns_; // number of instructions in function
187  Library::Ptr library_; // library to which function belongs
188  time_t ctime_ = 0; // time at which this object was created; zero if unknown
189  std::string cversion_; // ROSE version that created this object; empty if unknown
190 
191  protected:
192  Function(rose_addr_t address, const std::string &name, const std::string &demangledName, const std::string &hash,
193  size_t nInsns, time_t ctime, const std::string &cversion, const Library::Ptr &library);
194 
195  public:
201  static Ptr instance(rose_addr_t address, const std::string &name, const std::string &demangledName,
202  const std::string &hash, size_t nInsns, const Library::Ptr &library);
203 
209  static Ptr instance(rose_addr_t address, const std::string &name, const std::string &demangledName,
210  const std::string &hash, size_t nInsns, time_t creationTime, const std::string &creationVersion,
211  const Library::Ptr &library);
212 
213  public:
217  rose_addr_t address() const;
218 
222  const std::string& name() const;
223 
228  const std::string& demangledName() const;
229 
231  const std::string& hash() const;
232 
234  size_t nInsns() const;
235 
237  Library::Ptr library() const;
238 
240  time_t creationTime() const;
241 
243  const std::string& creationVersion() const;
244  };
245 
247  // Data members
249 public:
250  static Sawyer::Message::Facility mlog; // diagnostic facility for debugger
251 private:
252  Settings settings_;
253  std::string databaseUrl_; // name of database to which this object is communicating
254  Sawyer::Database::Connection db_; // the database, when connected
255 
256  // Cached info from the settings_.includeFile and settings_.excludeFile
257  boost::filesystem::path cachedIncludeFile_;
258  Sawyer::Container::Set<std::string> cachedIncludeNames_;
259  boost::filesystem::path cachedExcludeFile_;
260  Sawyer::Container::Set<std::string> cachedExcludeNames_;
261 
262  using LibraryCache = Sawyer::Container::Map<std::string /*hash*/, Library::Ptr>;
263  LibraryCache libraryCache_; // cache of library objects read from or written to database
264 
266  // Configuration settings
268 public:
272  const Settings& settings() const;
273  Settings& settings();
274  void settings(const Settings&);
282 
284  // Functions for attaching to the database.
286 public:
290  void connect(const std::string &databaseUrl);
291 
296  void createDatabase(const std::string &databaseUrl);
297 
299  // Functions that modify a database.
301 public:
309  Function::Ptr insertFunction(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
310  bool insertFunction(const Function::Ptr&);
319  size_t insertLibrary(const Library::Ptr&, const Partitioner2::PartitionerConstPtr&);
320 
322  // Functions for querying a database.
324 public:
328  Library::Ptr library(const std::string &hash);
329 
333  std::vector<Library::Ptr> libraries();
334 
342  std::vector<Function::Ptr> functions();
343  std::vector<Function::Ptr> functions(const Library::Ptr&);
347  std::vector<Function::Ptr> search(const Partitioner2::PartitionerConstPtr &partitioner, const Partitioner2::FunctionPtr&);
348 
350  // Utilities
352 public:
356  static void initDiagnostics();
357 
361  std::string hash(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&) const;
362 
364  static size_t nInsns(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
365 
366 private:
367  // Throws an Exception if the database driver version is not adequate.
368  void checkVersion();
369 
370  // Returns the database version number if there is one.
371  Sawyer::Optional<unsigned> version();
372 
373  // Create the tables needed by a new database.
374  void createTables();
375 
376  // Upgrade the database to the current version
377  void upgradeDatabase();
378 
379  // Create or update the properties of a library in the database.
380  void createLibrary(const Library::Ptr&);
381 
382  // Create or update the properties of a function in the database. It is not possible to update the address or library
383  // to which a function belongs with this call since those properties are what identifies the function -- attempting to
384  // change them simply creates a new function record without deleting the old one.
385  void createFunction(const Function::Ptr&);
386 
387  // Whether the current settings specifically include this function from consideration. Exactly one of f1 or f2 should be
388  // non-null.
389  bool isIncluded(const Function::Ptr&);
390  bool isIncluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
391 
392  // Whether the current settings exclude a function from consideration. Exactly one of f1 or f2 should be non-null.
393  bool isExcluded(const Function::Ptr&);
394  bool isExcluded(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
395 
396  // Whether to consider a function for insertion into a database or as a possible match of a database function to a
397  // non-database function.
398  bool isConsidered(const Function::Ptr&);
399  bool isConsidered(const Partitioner2::PartitionerConstPtr&, const Partitioner2::FunctionPtr&);
400 
401  // Load function names from files if necessary.
402  void cacheFiles();
403  void cacheNamesFromFile(const boost::filesystem::path &fileName, boost::filesystem::path &cachedFileName /*in,out*/,
404  Sawyer::Container::Set<std::string> &cachedNames /*out*/);
405 
406  // Return functions based on query.
407  std::vector<Function::Ptr> functions(Sawyer::Database::Statement);
408 };
409 
410 } // namespace
411 } // namespace
412 
413 #endif
414 #endif
Ordered set of values.
Definition: Set.h:52
std::string demangledName(std::string)
Compute demangled version of mangled name.
Collection of streams.
Definition: Message.h:1606
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.
A collection of related switch declarations.
Settings settings
Command-line settings for the rosebud tool.
Main namespace for the ROSE library.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Sawyer::SharedPointer< Function > FunctionPtr
Shared-ownership pointer for Function.
Sawyer::SharedPointer< const Partitioner > PartitionerConstPtr
Shared-ownership pointer for Partitioner.
Base class for reference counted objects.
Definition: SharedObject.h:64
void initDiagnostics()
Initialize diagnostics.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
Container associating values with keys.
Definition: Sawyer/Map.h:66
Hash hash(const std::vector< Ptr > &)
Hash zero or more expressions.