ROSE  0.11.50.0
Rose/BinaryAnalysis/Concolic/Database.h
1 #ifndef ROSE_BinaryAnalysis_Concolic_Database_H
2 #define ROSE_BinaryAnalysis_Concolic_Database_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_CONCOLIC_TESTING
5 #include <Rose/BinaryAnalysis/Concolic/BasicTypes.h>
6 
7 #include <boost/numeric/conversion/cast.hpp>
8 #include <ctype.h>
9 #include <rose_strtoull.h>
10 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/Types.h>
11 #include <Sawyer/BiMap.h>
12 #include <Sawyer/Database.h>
13 #include <Sawyer/Optional.h>
14 #include <string>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 namespace Concolic {
19 
32 class Database: public Sawyer::SharedObject, public Sawyer::SharedFromThis<Database>, boost::noncopyable {
33 public:
36 
37 private:
38 #if ROSE_CONCOLIC_DB_VERSION == 1
39  SqlDatabase::ConnectionPtr dbconn_; // connection to database
40 #else
41  Sawyer::Database::Connection connection_;
42 #endif
43 
44  // Memoization of ID to object mappings
49 
50  TestSuiteId testSuiteId_; // database scope is restricted to this single test suite
51 
52 protected:
53  Database();
54 
55 public:
56  ~Database();
57 
64  static Ptr instance(const std::string &url);
65 
67 #if ROSE_CONCOLIC_DB_VERSION == 1
68  SqlDatabase::ConnectionPtr connection() {
69  return dbconn_;
70  }
71 #else
72  Sawyer::Database::Connection connection() {
73  return connection_;
74  }
75 #endif
76 
87  static Ptr create(const std::string &url);
88  static Ptr create(const std::string &url, const std::string &testSuiteName);
91  //------------------------------------------------------------------------------------------------------------------------
92  // Test suites
93  //------------------------------------------------------------------------------------------------------------------------
94 
99  std::vector<TestSuiteId> testSuites();
100 
108  TestSuitePtr testSuite();
109  TestSuiteId testSuite(const TestSuitePtr&);
112  //------------------------------------------------------------------------------------------------------------------------
113  // Specimens
114  //------------------------------------------------------------------------------------------------------------------------
115 
120  std::vector<SpecimenId> specimens();
121 
123  std::vector<SpecimenId> specimens(TestSuiteId);
124 
126  void eraseSpecimens(TestSuiteId);
127 
128  //------------------------------------------------------------------------------------------------------------------------
129  // Test cases
130  //------------------------------------------------------------------------------------------------------------------------
131 
136  std::vector<TestCaseId> testCases();
137 
139  std::vector<TestCaseId> testCases(SpecimenId);
140 
142  void eraseTestCases(SpecimenId);
143 
144  //------------------------------------------------------------------------------------------------------------------------
145  // Execution events
146  //------------------------------------------------------------------------------------------------------------------------
147 
152  std::vector<ExecutionEventId> executionEvents();
153 
157  std::vector<ExecutionEventId> executionEvents(TestCaseId);
158 
160  size_t nExecutionEvents(TestCaseId);
161 
166  std::vector<ExecutionEventId> executionEvents(TestCaseId, uint64_t primaryKey);
167 
171  std::vector<ExecutionEventId> executionEventsSince(TestCaseId, ExecutionEventId startingAt);
172 
176  std::vector<uint64_t> executionEventKeyFrames(TestCaseId);
177 
179  void eraseExecutionEvents(TestCaseId);
180 
181  //------------------------------------------------------------------------------------------------------------------------
182  // Overloaded methods for all objects.
183  //------------------------------------------------------------------------------------------------------------------------
184 
191  TestSuitePtr object(TestSuiteId, Update update = Update::YES);
192  TestCasePtr object(TestCaseId, Update update = Update::YES);
193  SpecimenPtr object(SpecimenId, Update update = Update::YES);
194  ExecutionEventPtr object(ExecutionEventId, Update update = Update::YES);
197 #if ROSE_CONCOLIC_DB_VERSION == 1
198 
202  SpecimenPtr object_ns(SqlDatabase::TransactionPtr tx, SpecimenId id);
203 #endif
204 
213  TestSuiteId id(const TestSuitePtr&, Update update = Update::YES);
214  TestCaseId id(const TestCasePtr&, Update update = Update::YES);
215  SpecimenId id(const SpecimenPtr&, Update update = Update::YES);
216  ExecutionEventId id(const ExecutionEventPtr&, Update update = Update::YES);
222  TestSuiteId erase(TestSuiteId);
223  TestCaseId erase(TestCaseId);
224  SpecimenId erase(SpecimenId);
225  ExecutionEventId erase(ExecutionEventId);
232  template<class ObjectPointer>
233  typename ObjectTraits<typename ObjectPointer::Pointee>::Id
234  save(const ObjectPointer &obj) {
235  return id(obj);
236  }
237 
241  template<class ObjectPointer>
242  void save(const std::vector<ObjectPointer> &objects) {
243  for (auto object: objects)
244  save(object);
245  }
246 
250  template<class Id>
251  std::vector<typename Id::Pointer>
252  objects(const std::vector<Id> &ids, Update update = Update::YES) {
253  std::vector<typename Id::Pointer> retval;
254  for (auto id: ids)
255  retval.push_back(object(id, update));
256  return retval;
257  }
258 
262  template<class ObjectPointer>
263  std::vector<typename ObjectTraits<ObjectPointer>::Id>
264  ids(const std::vector<ObjectPointer> &objects, Update update = Update::YES) {
265  std::vector<typename ObjectTraits<ObjectPointer>::Id> retval;
266  for (auto object: objects)
267  retval.push_back(id(object, update));
268  return retval;
269  }
270 
271 #if ROSE_CONCOLIC_DB_VERSION == 1
272 
278  TestSuiteId id_ns(SqlDatabase::TransactionPtr, const TestSuitePtr&, Update::Flag update = Update::YES);
279  TestCaseId id_ns(SqlDatabase::TransactionPtr, const TestCasePtr&, Update::Flag update = Update::YES);
280  SpecimenId id_ns(SqlDatabase::TransactionPtr, const SpecimenPtr&, Update::Flag update = Update::YES);
281 #endif
282 
289  TestSuitePtr findTestSuite(const std::string &nameOrId);
290 
295  std::vector<SpecimenId> findSpecimensByName(const std::string &name);
296 
297  //------------------------------------------------------------------------------------------------------------------------
298  // Cached info about disassembly. This is large data. Each specimen has zero or one associated RBA data blob.
299  //------------------------------------------------------------------------------------------------------------------------
300 
307  bool rbaExists(SpecimenId);
308 
317  void saveRbaFile(const boost::filesystem::path&, SpecimenId);
318 
327  void extractRbaFile(const boost::filesystem::path&, SpecimenId);
328 
334  void eraseRba(SpecimenId);
335 
336  //------------------------------------------------------------------------------------------------------------------------
337  // Cached symbolic state.
338  //------------------------------------------------------------------------------------------------------------------------
339 
343  bool symbolicStateExists(TestCaseId);
344 
349  void saveSymbolicState(TestCaseId, const InstructionSemantics2::BaseSemantics::StatePtr&);
350 
354  InstructionSemantics2::BaseSemantics::StatePtr extractSymbolicState(TestCaseId);
355 
359  void eraseSymbolicState(TestCaseId);
360 
361  //------------------------------------------------------------------------------------------------------------------------
362  // Cached concrete execution results. This is large data. Each test case has zero or one associated concrete results.
363  //------------------------------------------------------------------------------------------------------------------------
364 
369  bool concreteResultExists(TestCaseId);
370 
378  void saveConcreteResult(const TestCasePtr&, const ConcreteExecutorResult*);
379 
384  std::unique_ptr<ConcreteExecutorResult> readConcreteResult(TestCaseId);
385 
390  std::vector<TestCaseId> needConcreteTesting(size_t n = UNLIMITED);
391 
393 
394 
395 
404  void assocTestCaseWithTestSuite(TestCaseId, TestSuiteId);
405 
410  std::vector<TestCaseId> needConcolicTesting(size_t n = UNLIMITED);
411 
412 #if 0 // [Robb Matzke 2020-07-15]
413  // Use saveConcreteResults instead, which allows concrete results to also be removed.
421  void insertConcreteResults(const TestCasePtr &testCase, const ConcreteExecutorResult& details);
422 #endif
423 
428  bool hasUntested();
429 
430 private:
431  static Ptr create(const std::string &url, const Sawyer::Optional<std::string> &testSuiteName);
432 };
433 
434 } // namespace
435 } // namespace
436 } // namespace
437 
438 
440 // Forward declarations for boost::serialization for classes that are serialized or de-serialized above. This must occur after
441 // the #include for the various boost archive headers, such as boost/archive/xml_oarchive.hpp. These must be at global scope.
443 
444 #include <boost/archive/xml_iarchive.hpp>
445 #include <boost/archive/xml_oarchive.hpp>
446 
447 #include <Rose/BinaryAnalysis/Concolic/LinuxExecutor.h>
448 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::Concolic::LinuxExecutor::Result);
449 
450 #include <Rose/BinaryAnalysis/Concolic/LinuxTraceExecutor.h>
451 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::Concolic::LinuxTraceExecutor::Result);
452 
453 #endif
454 #endif
Sawyer::SharedPointer< Node > Ptr
Shared-ownership pointer to an expression Node.
Definition: SymbolicExpr.h:156
const size_t UNLIMITED(-1)
Effictively unlimited size.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
boost::shared_ptr< Transaction > TransactionPtr
Shared-ownership pointer to a transaction.
Definition: SqlDatabase.h:147
Main namespace for the ROSE library.
One-to-one mapping between source and target values.
Definition: BiMap.h:26
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Creates SharedPointer from this.
Id id(const std::string &name)
Returns the ID for an attribute name.
boost::shared_ptr< Connection > ConnectionPtr
Shared-ownership pointer to a database connection.
Definition: SqlDatabase.h:137
Base class for reference counted objects.
Definition: SharedObject.h:64