ROSE  0.11.145.0
BinaryAnalysis/Concolic/BasicTypes.h
1 #ifndef ROSE_BinaryAnalysis_Concolic_BasicTypes_H
2 #define ROSE_BinaryAnalysis_Concolic_BasicTypes_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_CONCOLIC_TESTING
5 
6 // Subdirectory/subnamespace #include's are at the *end* of this file.
7 #include <Rose/StringUtility/Escape.h>
8 
9 #include <Rose/Exception.h>
10 #include <rose_strtoull.h>
11 
12 #include <Sawyer/Callbacks.h>
13 #include <Sawyer/Message.h>
14 #include <Sawyer/SharedPointer.h>
15 
16 #include <boost/shared_ptr.hpp>
17 #include <memory>
18 
19 namespace Rose {
20 namespace BinaryAnalysis {
21 namespace Concolic {
22 
24 // Flags and enums
26 
27 enum class Update { NO, YES };
28 
30 enum class ShowEvents {
31  NONE,
32  INPUT,
33  ALL
34 };
35 
36 enum class ShowAssertions { NO, YES };
37 
39 enum class InputType {
40  NONE,
41  ARGC,
42  ARGV,
43  ENVP,
44  SYSCALL_RET,
45  SHMEM_READ
46 };
47 
49 enum class IoDirection {
50  READ,
51  WRITE
52 };
53 
57 enum class When {
58  PRE,
59  POST
60 };
61 
63 enum class ConcolicPhase {
64  REPLAY,
65  EMULATION,
66  POST_EMULATION
67 };
68 
70 // Exceptions, errors, etc.
72 
75 
76 // Internal: called by Rose::Diagnostics::initialize
77 void initDiagnostics();
78 
80 class Exception: public Rose::Exception {
81 public:
82  explicit Exception(const std::string &mesg): Rose::Exception(mesg) {}
83  ~Exception() throw () {}
84 };
85 
89 bool isRunningInContainer();
90 
96 std::string toString(const SymbolicExpression::Ptr&, SymbolicExpression::Formatter&);
97 std::string toString(const SymbolicExpression::Ptr&);
98 std::string toString(uint64_t value, size_t nBits);
102 // Forward references
105 
106 class Architecture;
107 using ArchitecturePtr = Sawyer::SharedPointer<Architecture>;
108 
109 class ConcolicExecutor;
110 using ConcolicExecutorPtr = Sawyer::SharedPointer<ConcolicExecutor>;
111 
112 class ConcolicExecutorSettings;
113 
114 class ConcreteExecutor;
115 using ConcreteExecutorPtr = Sawyer::SharedPointer<ConcreteExecutor>;
116 
117 class ConcreteResult;
118 using ConcreteResultPtr = Sawyer::SharedPointer<ConcreteResult>;
119 
120 class Database;
121 using DatabasePtr = Sawyer::SharedPointer<Database>;
122 
123 namespace Emulation {
124  class RiscOperators;
125  using RiscOperatorsPtr = boost::shared_ptr<class RiscOperators>;
126 }
127 
128 class ExecutionEvent;
129 using ExecutionEventPtr = Sawyer::SharedPointer<ExecutionEvent>;
130 
131 class ExecutionLocation;
132 
133 class ExecutionManager;
134 using ExecutionManagerPtr = Sawyer::SharedPointer<ExecutionManager>;
135 
136 class InputVariables;
137 using InputVariablesPtr = Sawyer::SharedPointer<InputVariables>;
138 
139 class SharedMemoryCallback;
140 using SharedMemoryCallbackPtr = Sawyer::SharedPointer<SharedMemoryCallback>;
141 
142 using SharedMemoryCallbacks = Sawyer::Callbacks<SharedMemoryCallbackPtr>;
143 
144 class SharedMemoryContext;
145 
146 class Specimen;
147 using SpecimenPtr = Sawyer::SharedPointer<Specimen>;
148 
149 class SyscallCallback;
150 using SyscallCallbackPtr = std::shared_ptr<SyscallCallback>;
151 
152 using SyscallCallbacks = Sawyer::Callbacks<SyscallCallbackPtr>;
153 
154 class SyscallContext;
155 
156 class TestCase;
157 using TestCasePtr = Sawyer::SharedPointer<TestCase>;
158 
159 class TestSuite;
160 using TestSuitePtr = Sawyer::SharedPointer<TestSuite>;
161 
163 // Database
165 
167 template <class Tag>
168 class ObjectId: public Sawyer::Optional<size_t> {
169 public:
170  using Value = size_t;
171  using Super = Sawyer::Optional<Value>;
172  using Object = Tag;
173  using Pointer = Sawyer::SharedPointer<Tag>;
175  ObjectId() {}
176 
177  explicit
178  ObjectId(const Value& v)
179  : Super(v) {}
180 
181  ObjectId(const ObjectId& rhs)
182  : Super(rhs) {}
183 
184  explicit ObjectId(const Sawyer::Optional<size_t> &id)
185  : Super(id) {}
186 
192  explicit ObjectId(const std::string &s) {
193  char *rest = NULL;
194  uint64_t id = rose_strtoull(s.c_str(), &rest, 0);
195  while (*rest && isspace(*rest)) ++rest;
196  if (*rest)
197  throw Exception("invalid syntax for object ID: \"" + StringUtility::cEscape(s) + "\"");
198  try {
199  *this = boost::numeric_cast<Value>(id);
200  } catch (const boost::bad_numeric_cast&) {
201  throw Exception("parsed object ID out of range: \"" + StringUtility::cEscape(s) + "\"");
202  }
203  }
204 
206  ObjectId<Tag>& operator=(const ObjectId<Tag>& lhs) {
207  this->Super::operator=(lhs);
208  return *this;
209  }
210 
212  ObjectId<Tag>& operator=(const Value& v) {
213  this->Super::operator=(v);
214  return *this;
215  }
216 
217  explicit operator bool() const { // because it's not explicit in the super class due to C++03 support
218  return isEqual(Sawyer::Nothing()) ? false : true;
219  }
220 
222  template<class _Tag>
223  friend
224  bool operator<(const ObjectId<_Tag>& lhs, const ObjectId<_Tag>& rhs);
225 
226  // Useful for database operations
227  const Super& optional() const {
228  return *this;
229  }
230 };
231 
233 template<class Tag>
234 inline
235 bool operator<(const ObjectId<Tag>& lhs, const ObjectId<Tag>& rhs)
236 {
237  if (!rhs) return false;
238  if (!lhs) return true;
239 
240  return lhs.get() < rhs.get();
241 }
242 
243 using TestSuiteId = ObjectId<TestSuite>;
244 using SpecimenId = ObjectId<Specimen>;
245 using TestCaseId = ObjectId<TestCase>;
246 using ExecutionEventId = ObjectId<ExecutionEvent>;
251 template<class T>
252 struct ObjectTraits {
253  using Id = void;
254 };
255 
256 template<>
257 struct ObjectTraits<TestSuite> {
258  using Id = TestSuiteId;
259 };
260 
261 template<>
262 struct ObjectTraits<Specimen> {
263  using Id = SpecimenId;
264 };
265 
266 template<>
267 struct ObjectTraits<TestCase> {
268  using Id = TestCaseId;
269 };
270 
271 template<>
272 struct ObjectTraits<ExecutionEvent> {
273  using Id = ExecutionEventId;
274 };
275 
276 } // namespace
277 } // namespace
278 } // namespace
279 
280 #endif
281 
282 // #include's for subdirectories
283 #include <Rose/BinaryAnalysis/Concolic/Callback/BasicTypes.h>
284 #include <Rose/BinaryAnalysis/Concolic/I386Linux/BasicTypes.h>
285 #include <Rose/BinaryAnalysis/Concolic/M68kSystem/BasicTypes.h>
286 
287 #endif
const char * IoDirection(int64_t)
Convert Rose::BinaryAnalysis::Concolic::IoDirection enum constant to a string.
Collection of streams.
Definition: Message.h:1606
Only query an allocation.
Definition: CodeInserter.h:15
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
const char * ShowAssertions(int64_t)
Convert Rose::BinaryAnalysis::Concolic::ShowAssertions enum constant to a string. ...
Holds a value or nothing.
Definition: Optional.h:49
Main namespace for the ROSE library.
ROSE_UTIL_API std::string cEscape(const std::string &, char context= '"')
Escapes characters that are special to C/C++.
When
When something should be done.
Definition: ud/BasicTypes.h:23
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Object
The five kind of objects manipulated by Rose::CodeGen::API and associated Rose::CodeGen::Factory.
Definition: Object.h:11
ROSE_UTIL_API std::string toString(const Path &)
Convert a path to a string.
Allocate memory for real.
Definition: CodeInserter.h:16
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to concrete RISC operations.
const char * Update(int64_t)
Convert Rose::BinaryAnalysis::Concolic::Update enum constant to a string.
const char * Architecture(int64_t)
Convert Rose::BinaryAnalysis::Disassembler::Mips::Decoder::Architecture enum constant to a string...
const char * ConcolicPhase(int64_t)
Convert Rose::BinaryAnalysis::Concolic::ConcolicPhase enum constant to a string.
size_t Id
Attribute identification.
Definition: Attribute.h:140
const char * ShowEvents(int64_t)
Convert Rose::BinaryAnalysis::Concolic::ShowEvents enum constant to a string.
void initDiagnostics()
Initialize diagnostics.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
const char * InputType(int64_t)
Convert Rose::BinaryAnalysis::Concolic::InputType enum constant to a string.
Represents no value.
Definition: Optional.h:32
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9