ROSE  0.11.137.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/ByteOrder.h>
7 #include <Rose/BinaryAnalysis/Concolic/BasicTypes.h>
8 #include <Rose/BinaryAnalysis/Concolic/ExecutionLocation.h>
9 #include <Rose/BinaryAnalysis/Debugger/BasicTypes.h>
10 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/BasicTypes.h>
11 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
12 #include <Rose/BinaryAnalysis/Partitioner2/Engine.h>
13 #include <Rose/BinaryAnalysis/RegisterDescriptor.h>
14 #include <Rose/BinaryAnalysis/SmtSolver.h>
15 #include <Rose/Yaml.h>
16 
17 #include <Sawyer/BitVector.h>
18 
19 #include <boost/filesystem.hpp>
20 
21 namespace Rose {
22 namespace BinaryAnalysis {
23 namespace Concolic {
24 
26 class Architecture: public Sawyer::SharedObject, public Sawyer::SharedFromThis<Architecture> {
27 public:
29  using Ptr = ArchitecturePtr;
30 
32  using SystemCallMap = Sawyer::Container::Map<int /*syscall*/, SyscallCallbacks>;
33 
36 
37 private:
38  std::string name_; // architecture name for factory matching
39  DatabasePtr db_;
40  TestCaseId testCaseId_;
41  TestCasePtr testCase_;
43  Debugger::BasePtr debugger_;
44  ExecutionLocation currentLocation_; // incremented when the instruction begins execution
45  SystemCallMap systemCalls_; // callbacks for syscalls
46  SharedMemoryMap sharedMemory_; // callbacks for shared memory
47  InputVariablesPtr inputVariables_; // info about variables for events and inputs
48  Sawyer::Optional<rose_addr_t> scratchVa_; // scratch page for internal use in subordinate address space
49 
50 protected:
51  // See "instance" methods in subclasses
52  explicit Architecture(const std::string&); // for factories
53  Architecture(const DatabasePtr&, TestCaseId); // for non-factories
54 public:
55  virtual ~Architecture();
56 
58  // Factory
60 public:
69  static void registerFactory(const Ptr &factory);
70 
77  static bool deregisterFactory(const Ptr &factory);
78 
85  static std::vector<Ptr> registeredFactories();
86 
97  static Ptr forge(const DatabasePtr&, TestCaseId, const Yaml::Node &config);
98  static Ptr forge(const DatabasePtr&, const TestCasePtr&, const Yaml::Node &config);
102  virtual bool matchFactory(const Yaml::Node &config) const = 0;
103 
108  virtual Ptr instanceFromFactory(const DatabasePtr&, TestCaseId, const Yaml::Node &config) const = 0;
109 
115  bool isFactory() const;
116 
118  // Properties
120 public:
127  const std::string& name() const;
128  void name(const std::string&);
135  DatabasePtr database() const;
136 
141  TestCaseId testCaseId() const;
142 
147  TestCasePtr testCase() const;
148 
152  Partitioner2::PartitionerConstPtr partitioner() const;
153  void partitioner(const Partitioner2::PartitionerConstPtr&);
154 
160  Debugger::BasePtr debugger() const;
161  void debugger(const Debugger::BasePtr&);
170  Sawyer::Optional<rose_addr_t> scratchVa() const;
171  void scratchVa(const Sawyer::Optional<rose_addr_t>&);
186  ExecutionLocation currentLocation() const;
187  void currentLocation(const ExecutionLocation&);
199  const SystemCallMap& systemCalls() const;
200 
213  const SharedMemoryMap& sharedMemory() const;
214 
221  InputVariablesPtr inputVariables() const;
222  void inputVariables(const InputVariablesPtr&);
225  // Functions that can be called before execution starts.
228 public:
230  virtual Partitioner2::PartitionerPtr partition(const Partitioner2::EnginePtr&, const std::string &specimenName) = 0;
231 
235  virtual void configureSystemCalls() = 0;
236 
242  virtual void configureSharedMemory(const Yaml::Node &config);
243 
251  void sharedMemory(const SharedMemoryCallbackPtr&);
252  void sharedMemory(const SharedMemoryCallbackPtr&, const AddressInterval&);
256  void systemCalls(size_t syscallId, const SyscallCallbackPtr&);
257 
267  virtual void load(const boost::filesystem::path &tempDirectory) = 0;
268 
273  virtual bool isTerminated();
274 
278 
280  // Functions that create execution events. These query the concrete state but do not modify it.
282 public:
287  virtual std::vector<ExecutionEventPtr> createMemoryRestoreEvents() = 0;
288  std::vector<ExecutionEventPtr> createMemoryRestoreEvents(const MemoryMapPtr&);
289 
294  virtual std::vector<ExecutionEventPtr> createMemoryHashEvents() = 0;
295 
301  virtual std::vector<ExecutionEventPtr> createMemoryAdjustEvents(const MemoryMap::Ptr&, rose_addr_t insnVa) = 0;
302 
307  virtual std::vector<ExecutionEventPtr> createRegisterRestoreEvents();
308 
313  void saveEvents(const std::vector<ExecutionEventPtr>&, When);
314 
316  // High-level functions controlling concrete execution.
318 public:
324  size_t playAllEvents(const Partitioner2::PartitionerConstPtr&);
325 
332  virtual bool playEvent(const ExecutionEventPtr&);
333 
337  virtual void runToEvent(const ExecutionEventPtr&, const Partitioner2::PartitionerConstPtr&);
338 
342  uint64_t readMemoryUnsigned(rose_addr_t va, size_t nBytes);
343 
349  virtual std::string readCString(rose_addr_t va, size_t maxBytes = UNLIMITED);
350 
352  // Low-level functions controlling concrete execution.
354 public:
358  virtual ByteOrder::Endianness memoryByteOrder() = 0;
359 
363  virtual rose_addr_t ip();
364  virtual void ip(rose_addr_t);
371  virtual void mapMemory(const AddressInterval &where, unsigned permissions);
372 
376  virtual void unmapMemory(const AddressInterval &where);
377 
381  virtual size_t writeMemory(rose_addr_t startVa, const std::vector<uint8_t> &bytes);
382 
387  virtual std::vector<uint8_t> readMemory(rose_addr_t startVa, size_t nBytes);
388 
395  virtual void writeRegister(RegisterDescriptor, uint64_t value);
396  virtual void writeRegister(RegisterDescriptor, const Sawyer::Container::BitVector&);
400  virtual Sawyer::Container::BitVector readRegister(RegisterDescriptor);
401 
406  virtual void checkInstruction(SgAsmInstruction*);
407 
412  virtual void advanceExecution(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr&);
413 
419  virtual void executeInstruction(const Partitioner2::PartitionerConstPtr&);
420  virtual void executeInstruction(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr&, SgAsmInstruction*);
427  const ExecutionLocation& nextInstructionLocation();
428 
435  const ExecutionLocation& nextEventLocation(When);
442  std::vector<ExecutionEventPtr> getRelatedEvents(const ExecutionEventPtr&) const;
443 
445  // Functions related to symbolic states.
447 public:
455  virtual void createInputVariables(const Partitioner2::PartitionerConstPtr&, const Emulation::RiscOperatorsPtr&,
456  const SmtSolver::Ptr&) = 0;
457 
475  virtual void restoreInputVariables(const Partitioner2::PartitionerConstPtr&, const Emulation::RiscOperatorsPtr&,
476  const SmtSolver::Ptr&);
477 
479  // Functions related to operating system emulation
481 public:
486  virtual void systemCall(const Partitioner2::PartitionerConstPtr&,
488 
501  virtual std::pair<ExecutionEventPtr, SymbolicExpressionPtr>
502  sharedMemoryRead(const SharedMemoryCallbacks&, const Partitioner2::PartitionerConstPtr&, const Emulation::RiscOperatorsPtr&,
503  rose_addr_t memVa, size_t nBytes);
504  virtual bool
505  sharedMemoryWrite(const SharedMemoryCallbacks&, const Partitioner2::PartitionerConstPtr&, const Emulation::RiscOperatorsPtr&,
506  rose_addr_t memVa, const InstructionSemantics::BaseSemantics::SValuePtr&);
513  virtual void runSharedMemoryPostCallbacks(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
514 
542  void fixupSharedMemoryEvents(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
543 
545  void printSharedMemoryEvents(const ExecutionEventPtr &sharedMemoryEvent, const Emulation::RiscOperatorsPtr&);
546 
548  virtual void sharedMemoryAccessPost(const Partitioner2::PartitionerConstPtr&, const Emulation::RiscOperatorsPtr&);
549 };
550 
551 } // namespace
552 } // namespace
553 } // namespace
554 
555 #endif
556 #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.
Sawyer::SharedPointer< MemoryMap > MemoryMapPtr
Reference counting pointer.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
std::vector< BasePtr > registeredFactories()
Returns all registered disassembler factories.
MemoryMapPtr Ptr
Reference counting pointer.
Definition: MemoryMap.h:115
Sawyer::SharedPointer< Partitioner > PartitionerPtr
Shared-ownership pointer for Partitioner.
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.
Sawyer::SharedPointer< const Partitioner > PartitionerConstPtr
Shared-ownership pointer for Partitioner.
const char * Architecture(int64_t)
Convert Rose::BinaryAnalysis::Disassembler::Mips::Decoder::Architecture enum constant to a string...
Sawyer::SharedPointer< Engine > EnginePtr
Shared-ownership pointer for Engine.
bool deregisterFactory(const BasePtr &)
Remove a disassembler factory from the registry.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
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.
void registerFactory(const BasePtr &)
Append a disassembler factory to the registry.
Container associating values with keys.
Definition: Sawyer/Map.h:66