Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:47

0001 //===-- IRMemoryMap.h -------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_EXPRESSION_IRMEMORYMAP_H
0010 #define LLDB_EXPRESSION_IRMEMORYMAP_H
0011 
0012 #include "lldb/Utility/DataBufferHeap.h"
0013 #include "lldb/Utility/UserID.h"
0014 #include "lldb/lldb-public.h"
0015 
0016 #include <map>
0017 
0018 namespace lldb_private {
0019 
0020 /// \class IRMemoryMap IRMemoryMap.h "lldb/Expression/IRMemoryMap.h"
0021 /// Encapsulates memory that may exist in the process but must
0022 ///     also be available in the host process.
0023 ///
0024 /// This class encapsulates a group of memory objects that must be readable or
0025 /// writable from the host process regardless of whether the process exists.
0026 /// This allows the IR interpreter as well as JITted code to access the same
0027 /// memory.  All allocations made by this class are represented as disjoint
0028 /// intervals.
0029 ///
0030 /// Point queries against this group of memory objects can be made by the
0031 /// address in the tar at which they reside.  If the inferior does not exist,
0032 /// allocations still get made-up addresses.  If an inferior appears at some
0033 /// point, then those addresses need to be re-mapped.
0034 class IRMemoryMap {
0035 public:
0036   IRMemoryMap(lldb::TargetSP target_sp);
0037   ~IRMemoryMap();
0038 
0039   enum AllocationPolicy : uint8_t {
0040     eAllocationPolicyInvalid =
0041         0, ///< It is an error for an allocation to have this policy.
0042     eAllocationPolicyHostOnly, ///< This allocation was created in the host and
0043                                ///will never make it into the process.
0044     ///< It is an error to create other types of allocations while such
0045     ///allocations exist.
0046     eAllocationPolicyMirror, ///< The intent is that this allocation exist both
0047                              ///in the host and the process and have
0048                              ///< the same content in both.
0049     eAllocationPolicyProcessOnly ///< The intent is that this allocation exist
0050                                  ///only in the process.
0051   };
0052 
0053   lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions,
0054                       AllocationPolicy policy, bool zero_memory, Status &error);
0055   void Leak(lldb::addr_t process_address, Status &error);
0056   void Free(lldb::addr_t process_address, Status &error);
0057 
0058   void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes,
0059                    size_t size, Status &error);
0060   void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar,
0061                            size_t size, Status &error);
0062   void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address,
0063                             Status &error);
0064   void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size,
0065                   Status &error);
0066   void ReadScalarFromMemory(Scalar &scalar, lldb::addr_t process_address,
0067                             size_t size, Status &error);
0068   void ReadPointerFromMemory(lldb::addr_t *address,
0069                              lldb::addr_t process_address, Status &error);
0070   bool GetAllocSize(lldb::addr_t address, size_t &size);
0071   void GetMemoryData(DataExtractor &extractor, lldb::addr_t process_address,
0072                      size_t size, Status &error);
0073 
0074   lldb::ByteOrder GetByteOrder();
0075   uint32_t GetAddressByteSize();
0076 
0077   // This function can return NULL.
0078   ExecutionContextScope *GetBestExecutionContextScope() const;
0079 
0080   lldb::TargetSP GetTarget() { return m_target_wp.lock(); }
0081 
0082 protected:
0083   // This function should only be used if you know you are using the JIT. Any
0084   // other cases should use GetBestExecutionContextScope().
0085 
0086   lldb::ProcessWP &GetProcessWP() { return m_process_wp; }
0087 
0088 private:
0089   struct Allocation {
0090     lldb::addr_t
0091         m_process_alloc; ///< The (unaligned) base for the remote allocation.
0092     lldb::addr_t
0093         m_process_start; ///< The base address of the allocation in the process.
0094     size_t m_size;       ///< The size of the requested allocation.
0095     DataBufferHeap m_data;
0096 
0097     /// Flags. Keep these grouped together to avoid structure padding.
0098     AllocationPolicy m_policy;
0099     bool m_leak;
0100     uint8_t m_permissions; ///< The access permissions on the memory in the
0101                            /// process. In the host, the memory is always
0102                            /// read/write.
0103     uint8_t m_alignment;   ///< The alignment of the requested allocation.
0104 
0105   public:
0106     Allocation(lldb::addr_t process_alloc, lldb::addr_t process_start,
0107                size_t size, uint32_t permissions, uint8_t alignment,
0108                AllocationPolicy m_policy);
0109 
0110     Allocation(const Allocation &) = delete;
0111     const Allocation &operator=(const Allocation &) = delete;
0112   };
0113 
0114   static_assert(sizeof(Allocation) <=
0115                     (4 * sizeof(lldb::addr_t)) + sizeof(DataBufferHeap),
0116                 "IRMemoryMap::Allocation is larger than expected");
0117 
0118   lldb::ProcessWP m_process_wp;
0119   lldb::TargetWP m_target_wp;
0120   typedef std::map<lldb::addr_t, Allocation> AllocationMap;
0121   AllocationMap m_allocations;
0122 
0123   lldb::addr_t FindSpace(size_t size);
0124   bool ContainsHostOnlyAllocations();
0125   AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size);
0126 
0127   // Returns true if the given allocation intersects any allocation in the
0128   // memory map.
0129   bool IntersectsAllocation(lldb::addr_t addr, size_t size) const;
0130 
0131   // Returns true if the two given allocations intersect each other.
0132   static bool AllocationsIntersect(lldb::addr_t addr1, size_t size1,
0133                                    lldb::addr_t addr2, size_t size2);
0134 };
0135 }
0136 
0137 #endif