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