ROSE  0.11.101.0
Architecture.h
1 #ifndef ROSE_BinaryAnalysis_Concolic_Architecture_H
2 #define ROSE_BinaryAnalysis_Concolic_Architecture_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_CONCOLIC_TESTING
5 
6 #include <Rose/BinaryAnalysis/Concolic/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/Concolic/ExecutionLocation.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/Types.h>
9 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
10 #include <Rose/BinaryAnalysis/RegisterDescriptor.h>
11 #include <Rose/BinaryAnalysis/SmtSolver.h>
12 #include <ByteOrder.h>
13 
14 #include <Sawyer/BitVector.h>
15 
16 #include <boost/filesystem.hpp>
17 
18 namespace Rose {
19 namespace BinaryAnalysis {
20 namespace Concolic {
21 
23 class Architecture: public Sawyer::SharedObject, public Sawyer::SharedFromThis<Architecture> {
24 public:
26  using Ptr = ArchitecturePtr;
27 
29  using SystemCallMap = Sawyer::Container::Map<int /*syscall*/, SyscallCallbacks>;
30 
33 
34 private:
35  DatabasePtr db_;
36  TestCaseId testCaseId_;
37  TestCasePtr testCase_;
38  const Partitioner2::Partitioner &partitioner_;
39  ExecutionLocation currentLocation_; // incremented when the instruction begins execution
40  SystemCallMap systemCalls_; // callbacks for syscalls
41  SharedMemoryMap sharedMemory_; // callbacks for shared memory
42  InputVariablesPtr inputVariables_; // info about variables for events and inputs
43 
44 protected:
45  // See "instance" methods in subclasses
46  Architecture(const DatabasePtr&, TestCaseId, const Partitioner2::Partitioner&);
47 public:
48  virtual ~Architecture();
49 
51  // Properties
53 public:
58  DatabasePtr database() const;
59 
64  TestCaseId testCaseId() const;
65 
70  TestCasePtr testCase() const;
71 
75  const Partitioner2::Partitioner& partitioner() const;
76 
89  ExecutionLocation currentLocation() const;
90  void currentLocation(const ExecutionLocation&);
102  const SystemCallMap& systemCalls() const;
103 
116  const SharedMemoryMap& sharedMemory() const;
117 
124  InputVariablesPtr inputVariables() const;
125  void inputVariables(const InputVariablesPtr&);
128  // Functions that can be called before execution starts.
131 public:
135  virtual void configureSystemCalls() = 0;
136 
141  virtual void configureSharedMemory() = 0;
142 
144  void sharedMemory(const AddressInterval&, const SharedMemoryCallbackPtr&);
145 
147  void systemCalls(size_t syscallId, const SyscallCallbackPtr&);
148 
158  virtual void load(const boost::filesystem::path &tempDirectory) = 0;
159 
164  virtual bool isTerminated() = 0;
165 
167  // Functions that create execution events. These query the concrete state but do not modify it.
169 public:
174  virtual std::vector<ExecutionEventPtr> createMemoryRestoreEvents() = 0;
175 
180  virtual std::vector<ExecutionEventPtr> createMemoryHashEvents() = 0;
181 
187  virtual std::vector<ExecutionEventPtr> createMemoryAdjustEvents(const MemoryMap::Ptr&, rose_addr_t insnVa) = 0;
188 
193  virtual std::vector<ExecutionEventPtr> createRegisterRestoreEvents() = 0;
194 
199  void saveEvents(const std::vector<ExecutionEventPtr>&, When);
200 
202  // High-level functions controlling concrete execution.
204 public:
210  size_t playAllEvents(const Partitioner2::Partitioner&);
211 
218  virtual bool playEvent(const ExecutionEventPtr&);
219 
223  virtual void runToEvent(const ExecutionEventPtr&, const Partitioner2::Partitioner&);
224 
228  uint64_t readMemoryUnsigned(rose_addr_t va, size_t nBytes);
229 
235  virtual std::string readCString(rose_addr_t va, size_t maxBytes = UNLIMITED);
236 
238  // Low-level functions controlling concrete execution.
240 public:
244  virtual ByteOrder::Endianness memoryByteOrder() = 0;
245 
249  virtual rose_addr_t ip() = 0;
250  virtual void ip(rose_addr_t) = 0;
257  virtual void mapMemory(const AddressInterval &where, unsigned permissions) {}
258 
262  virtual void unmapMemory(const AddressInterval &where) {}
263 
267  virtual size_t writeMemory(rose_addr_t startVa, const std::vector<uint8_t> &bytes) = 0;
268 
273  virtual std::vector<uint8_t> readMemory(rose_addr_t startVa, size_t nBytes) = 0;
274 
281  virtual void writeRegister(RegisterDescriptor, uint64_t value) = 0;
282  virtual void writeRegister(RegisterDescriptor, const Sawyer::Container::BitVector&) = 0;
286  virtual Sawyer::Container::BitVector readRegister(RegisterDescriptor) = 0;
287 
293  virtual void executeInstruction(const Partitioner2::Partitioner&) = 0;
294  virtual void executeInstruction(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr&, SgAsmInstruction*) = 0;
301  const ExecutionLocation& nextInstructionLocation();
302 
309  const ExecutionLocation& nextEventLocation(When);
316  std::vector<ExecutionEventPtr> getRelatedEvents(const ExecutionEventPtr&) const;
317 
319  // Functions related to symbolic states.
321 public:
329  virtual void createInputVariables(const Partitioner2::Partitioner&, const Emulation::RiscOperatorsPtr&,
330  const SmtSolver::Ptr&) = 0;
331 
349  virtual void restoreInputVariables(const Partitioner2::Partitioner&, const Emulation::RiscOperatorsPtr&,
350  const SmtSolver::Ptr&);
351 
353  // Functions related to operating system emulation
355 public:
360  virtual void systemCall(const Partitioner2::Partitioner&, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr&) {}
361 
367  virtual std::pair<ExecutionEventPtr, SymbolicExpressionPtr>
368  sharedMemoryAccess(const SharedMemoryCallbacks&, const Partitioner2::Partitioner&, const Emulation::RiscOperatorsPtr&,
369  rose_addr_t memVa, size_t nBytes);
370 
375  virtual void runSharedMemoryPostCallbacks(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
376 
404  void fixupSharedMemoryEvents(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
405 
407  void printSharedMemoryEvents(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
408 
410  virtual void sharedMemoryAccessPost(const Partitioner2::Partitioner&, const Emulation::RiscOperatorsPtr&);
411 };
412 
413 } // namespace
414 } // namespace
415 } // namespace
416 
417 #endif
418 #endif
SmtSolverPtr Ptr
Reference counting pointer for SMT solvers.
Definition: SmtSolver.h:45
An associative container whose keys are non-overlapping intervals.
Definition: IntervalMap.h:171
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Base class for machine instructions.
Main namespace for the ROSE library.
const size_t UNLIMITED(static_cast< size_t >(-1))
Effictively unlimited size.
MemoryMapPtr Ptr
Reference counting pointer.
Definition: MemoryMap.h:115
Creates SharedPointer from this.
ROSE_DLL_API void load(SgProject *project, std::list< std::string > const &filepaths)
Load ASTs that have been saved to files.
Binary analysis.
Base class for reference counted objects.
Definition: SharedObject.h:64
Range of values delimited by endpoints.
Definition: Interval.h:33
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
Container associating values with keys.
Definition: Sawyer/Map.h:66
const char * Architecture(int64_t)
Convert Rose::BinaryAnalysis::DisassemblerMips::Mips32::Architecture enum constant to a string...