1 //===-- IRExecutionUnit.h ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef lldb_IRMemoryMap_h_ 11 #define lldb_IRMemoryMap_h_ 12 13 #include "lldb/Utility/DataBufferHeap.h" 14 #include "lldb/Utility/UserID.h" 15 #include "lldb/lldb-public.h" 16 17 #include <map> 18 19 namespace lldb_private { 20 21 //---------------------------------------------------------------------- 22 /// @class IRMemoryMap IRMemoryMap.h "lldb/Expression/IRMemoryMap.h" 23 /// Encapsulates memory that may exist in the process but must 24 /// also be available in the host process. 25 /// 26 /// This class encapsulates a group of memory objects that must be readable or 27 /// writable from the host process regardless of whether the process exists. 28 /// This allows the IR interpreter as well as JITted code to access the same 29 /// memory. All allocations made by this class are represented as disjoint 30 /// intervals. 31 /// 32 /// Point queries against this group of memory objects can be made by the 33 /// address in the tar at which they reside. If the inferior does not exist, 34 /// allocations still get made-up addresses. If an inferior appears at some 35 /// point, then those addresses need to be re-mapped. 36 //---------------------------------------------------------------------- 37 class IRMemoryMap { 38 public: 39 IRMemoryMap(lldb::TargetSP target_sp); 40 ~IRMemoryMap(); 41 42 enum AllocationPolicy : uint8_t { 43 eAllocationPolicyInvalid = 44 0, ///< It is an error for an allocation to have this policy. 45 eAllocationPolicyHostOnly, ///< This allocation was created in the host and 46 ///will never make it into the process. 47 ///< It is an error to create other types of allocations while such 48 ///allocations exist. 49 eAllocationPolicyMirror, ///< The intent is that this allocation exist both 50 ///in the host and the process and have 51 ///< the same content in both. 52 eAllocationPolicyProcessOnly ///< The intent is that this allocation exist 53 ///only in the process. 54 }; 55 56 lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions, 57 AllocationPolicy policy, bool zero_memory, Status &error); 58 void Leak(lldb::addr_t process_address, Status &error); 59 void Free(lldb::addr_t process_address, Status &error); 60 61 void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes, 62 size_t size, Status &error); 63 void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar, 64 size_t size, Status &error); 65 void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address, 66 Status &error); 67 void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size, 68 Status &error); 69 void ReadScalarFromMemory(Scalar &scalar, lldb::addr_t process_address, 70 size_t size, Status &error); 71 void ReadPointerFromMemory(lldb::addr_t *address, 72 lldb::addr_t process_address, Status &error); 73 bool GetAllocSize(lldb::addr_t address, size_t &size); 74 void GetMemoryData(DataExtractor &extractor, lldb::addr_t process_address, 75 size_t size, Status &error); 76 77 lldb::ByteOrder GetByteOrder(); 78 uint32_t GetAddressByteSize(); 79 80 // This function can return NULL. 81 ExecutionContextScope *GetBestExecutionContextScope() const; 82 GetTarget()83 lldb::TargetSP GetTarget() { return m_target_wp.lock(); } 84 85 protected: 86 // This function should only be used if you know you are using the JIT. Any 87 // other cases should use GetBestExecutionContextScope(). 88 GetProcessWP()89 lldb::ProcessWP &GetProcessWP() { return m_process_wp; } 90 91 private: 92 struct Allocation { 93 lldb::addr_t 94 m_process_alloc; ///< The (unaligned) base for the remote allocation. 95 lldb::addr_t 96 m_process_start; ///< The base address of the allocation in the process. 97 size_t m_size; ///< The size of the requested allocation. 98 DataBufferHeap m_data; 99 100 /// Flags. Keep these grouped together to avoid structure padding. 101 AllocationPolicy m_policy; 102 bool m_leak; 103 uint8_t m_permissions; ///< The access permissions on the memory in the 104 /// process. In the host, the memory is always 105 /// read/write. 106 uint8_t m_alignment; ///< The alignment of the requested allocation. 107 108 public: 109 Allocation(lldb::addr_t process_alloc, lldb::addr_t process_start, 110 size_t size, uint32_t permissions, uint8_t alignment, 111 AllocationPolicy m_policy); 112 113 DISALLOW_COPY_AND_ASSIGN(Allocation); 114 }; 115 116 static_assert(sizeof(Allocation) <= 117 (4 * sizeof(lldb::addr_t)) + sizeof(DataBufferHeap), 118 "IRMemoryMap::Allocation is larger than expected"); 119 120 lldb::ProcessWP m_process_wp; 121 lldb::TargetWP m_target_wp; 122 typedef std::map<lldb::addr_t, Allocation> AllocationMap; 123 AllocationMap m_allocations; 124 125 lldb::addr_t FindSpace(size_t size); 126 bool ContainsHostOnlyAllocations(); 127 AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size); 128 129 // Returns true if the given allocation intersects any allocation in the 130 // memory map. 131 bool IntersectsAllocation(lldb::addr_t addr, size_t size) const; 132 133 // Returns true if the two given allocations intersect each other. 134 static bool AllocationsIntersect(lldb::addr_t addr1, size_t size1, 135 lldb::addr_t addr2, size_t size2); 136 }; 137 } 138 139 #endif 140