ROSE  0.11.101.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/InstructionSemantics/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  Sawyer::Database::Connection connection_;
39 
40  // Memoization of ID to object mappings
45 
46  TestSuiteId testSuiteId_; // database scope is restricted to this single test suite
47 
48 protected:
49  Database();
50 
51 public:
52  ~Database();
53 
60  static Ptr instance(const std::string &url);
61 
63  Sawyer::Database::Connection connection() {
64  return connection_;
65  }
66 
77  static Ptr create(const std::string &url);
78  static Ptr create(const std::string &url, const std::string &testSuiteName);
81  //------------------------------------------------------------------------------------------------------------------------
82  // Test suites
83  //------------------------------------------------------------------------------------------------------------------------
84 
89  std::vector<TestSuiteId> testSuites();
90 
98  TestSuitePtr testSuite();
99  TestSuiteId testSuite(const TestSuitePtr&);
102  //------------------------------------------------------------------------------------------------------------------------
103  // Specimens
104  //------------------------------------------------------------------------------------------------------------------------
105 
110  std::vector<SpecimenId> specimens();
111 
113  std::vector<SpecimenId> specimens(TestSuiteId);
114 
116  void eraseSpecimens(TestSuiteId);
117 
118  //------------------------------------------------------------------------------------------------------------------------
119  // Test cases
120  //------------------------------------------------------------------------------------------------------------------------
121 
126  std::vector<TestCaseId> testCases();
127 
129  std::vector<TestCaseId> testCases(SpecimenId);
130 
132  void eraseTestCases(SpecimenId);
133 
134  //------------------------------------------------------------------------------------------------------------------------
135  // Execution events
136  //------------------------------------------------------------------------------------------------------------------------
137 
142  std::vector<ExecutionEventId> executionEvents();
143 
147  std::vector<ExecutionEventId> executionEvents(TestCaseId);
148 
150  size_t nExecutionEvents(TestCaseId);
151 
156  std::vector<ExecutionEventId> executionEvents(TestCaseId, uint64_t primaryKey);
157 
161  std::vector<ExecutionEventId> executionEventsSince(TestCaseId, ExecutionEventId startingAt);
162 
166  std::vector<uint64_t> executionEventKeyFrames(TestCaseId);
167 
169  void eraseExecutionEvents(TestCaseId);
170 
171  //------------------------------------------------------------------------------------------------------------------------
172  // Overloaded methods for all objects.
173  //------------------------------------------------------------------------------------------------------------------------
174 
181  TestSuitePtr object(TestSuiteId, Update update = Update::YES);
182  TestCasePtr object(TestCaseId, Update update = Update::YES);
183  SpecimenPtr object(SpecimenId, Update update = Update::YES);
184  ExecutionEventPtr object(ExecutionEventId, Update update = Update::YES);
195  TestSuiteId id(const TestSuitePtr&, Update update = Update::YES);
196  TestCaseId id(const TestCasePtr&, Update update = Update::YES);
197  SpecimenId id(const SpecimenPtr&, Update update = Update::YES);
198  ExecutionEventId id(const ExecutionEventPtr&, Update update = Update::YES);
204  TestSuiteId erase(TestSuiteId);
205  TestCaseId erase(TestCaseId);
206  SpecimenId erase(SpecimenId);
207  ExecutionEventId erase(ExecutionEventId);
214  template<class ObjectPointer>
215  typename ObjectTraits<typename ObjectPointer::Pointee>::Id
216  save(const ObjectPointer &obj) {
217  return id(obj);
218  }
219 
223  template<class ObjectPointer>
224  void save(const std::vector<ObjectPointer> &objects) {
225  for (auto object: objects)
226  save(object);
227  }
228 
232  template<class Id>
233  std::vector<typename Id::Pointer>
234  objects(const std::vector<Id> &ids, Update update = Update::YES) {
235  std::vector<typename Id::Pointer> retval;
236  for (auto id: ids)
237  retval.push_back(object(id, update));
238  return retval;
239  }
240 
244  template<class ObjectPointer>
245  std::vector<typename ObjectTraits<ObjectPointer>::Id>
246  ids(const std::vector<ObjectPointer> &objects, Update update = Update::YES) {
247  std::vector<typename ObjectTraits<ObjectPointer>::Id> retval;
248  for (auto object: objects)
249  retval.push_back(id(object, update));
250  return retval;
251  }
252 
259  TestSuitePtr findTestSuite(const std::string &nameOrId);
260 
265  std::vector<SpecimenId> findSpecimensByName(const std::string &name);
266 
267  //------------------------------------------------------------------------------------------------------------------------
268  // Cached info about disassembly. This is large data. Each specimen has zero or one associated RBA data blob.
269  //------------------------------------------------------------------------------------------------------------------------
270 
277  bool rbaExists(SpecimenId);
278 
287  void saveRbaFile(const boost::filesystem::path&, SpecimenId);
288 
297  void extractRbaFile(const boost::filesystem::path&, SpecimenId);
298 
304  void eraseRba(SpecimenId);
305 
306  //------------------------------------------------------------------------------------------------------------------------
307  // Cached symbolic state.
308  //------------------------------------------------------------------------------------------------------------------------
309 
313  bool symbolicStateExists(TestCaseId);
314 
319  void saveSymbolicState(TestCaseId, const InstructionSemantics::BaseSemantics::StatePtr&);
320 
324  InstructionSemantics::BaseSemantics::StatePtr extractSymbolicState(TestCaseId);
325 
329  void eraseSymbolicState(TestCaseId);
330 
331  //------------------------------------------------------------------------------------------------------------------------
332  // Cached concrete execution results. This is large data. Each test case has zero or one associated concrete results.
333  //------------------------------------------------------------------------------------------------------------------------
334 
339  bool concreteResultExists(TestCaseId);
340 
348  void saveConcreteResult(const TestCasePtr&, const ConcreteExecutorResult*);
349 
354  std::unique_ptr<ConcreteExecutorResult> readConcreteResult(TestCaseId);
355 
360  std::vector<TestCaseId> needConcreteTesting(size_t n = UNLIMITED);
361 
363 
364 
365 
374  void assocTestCaseWithTestSuite(TestCaseId, TestSuiteId);
375 
380  std::vector<TestCaseId> needConcolicTesting(size_t n = UNLIMITED);
381 
382 #if 0 // [Robb Matzke 2020-07-15]
383  // Use saveConcreteResults instead, which allows concrete results to also be removed.
391  void insertConcreteResults(const TestCasePtr &testCase, const ConcreteExecutorResult& details);
392 #endif
393 
398  bool hasUntested();
399 
401  static std::string timestamp();
402 
403 private:
404  static Ptr create(const std::string &url, const Sawyer::Optional<std::string> &testSuiteName);
405 };
406 
407 } // namespace
408 } // namespace
409 } // namespace
410 
411 
413 // Forward declarations for boost::serialization for classes that are serialized or de-serialized above. This must occur after
414 // the #include for the various boost archive headers, such as boost/archive/xml_oarchive.hpp. These must be at global scope.
416 
417 #include <boost/archive/xml_iarchive.hpp>
418 #include <boost/archive/xml_oarchive.hpp>
419 
420 #include <Rose/BinaryAnalysis/Concolic/LinuxExecutor.h>
421 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::Concolic::LinuxExecutor::Result);
422 
423 #include <Rose/BinaryAnalysis/Concolic/LinuxTraceExecutor.h>
424 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::Concolic::LinuxTraceExecutor::Result);
425 
426 #endif
427 #endif
Main namespace for the ROSE library.
const size_t UNLIMITED(static_cast< size_t >(-1))
Effictively unlimited size.
One-to-one mapping between source and target values.
Definition: BiMap.h:26
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
Creates SharedPointer from this.
Id id(const std::string &name)
Returns the ID for an attribute name.
Binary analysis.
Base class for reference counted objects.
Definition: SharedObject.h:64
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.