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