IRMemoryMap.h 5.6 KB

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