SectionMemoryManager.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- 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. // This file contains the declaration of a section-based memory manager used by
  10. // the MCJIT execution engine and RuntimeDyld.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
  14. #define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
  17. #include "llvm/Support/Memory.h"
  18. #include <cstdint>
  19. #include <string>
  20. #include <system_error>
  21. namespace llvm {
  22. /// This is a simple memory manager which implements the methods called by
  23. /// the RuntimeDyld class to allocate memory for section-based loading of
  24. /// objects, usually those generated by the MCJIT execution engine.
  25. ///
  26. /// This memory manager allocates all section memory as read-write. The
  27. /// RuntimeDyld will copy JITed section memory into these allocated blocks
  28. /// and perform any necessary linking and relocations.
  29. ///
  30. /// Any client using this memory manager MUST ensure that section-specific
  31. /// page permissions have been applied before attempting to execute functions
  32. /// in the JITed object. Permissions can be applied either by calling
  33. /// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
  34. /// directly. Clients of MCJIT should call MCJIT::finalizeObject.
  35. class SectionMemoryManager : public RTDyldMemoryManager {
  36. public:
  37. /// This enum describes the various reasons to allocate pages from
  38. /// allocateMappedMemory.
  39. enum class AllocationPurpose {
  40. Code,
  41. ROData,
  42. RWData,
  43. };
  44. /// Implementations of this interface are used by SectionMemoryManager to
  45. /// request pages from the operating system.
  46. class MemoryMapper {
  47. public:
  48. /// This method attempts to allocate \p NumBytes bytes of virtual memory for
  49. /// \p Purpose. \p NearBlock may point to an existing allocation, in which
  50. /// case an attempt is made to allocate more memory near the existing block.
  51. /// The actual allocated address is not guaranteed to be near the requested
  52. /// address. \p Flags is used to set the initial protection flags for the
  53. /// block of the memory. \p EC [out] returns an object describing any error
  54. /// that occurs.
  55. ///
  56. /// This method may allocate more than the number of bytes requested. The
  57. /// actual number of bytes allocated is indicated in the returned
  58. /// MemoryBlock.
  59. ///
  60. /// The start of the allocated block must be aligned with the system
  61. /// allocation granularity (64K on Windows, page size on Linux). If the
  62. /// address following \p NearBlock is not so aligned, it will be rounded up
  63. /// to the next allocation granularity boundary.
  64. ///
  65. /// \r a non-null MemoryBlock if the function was successful, otherwise a
  66. /// null MemoryBlock with \p EC describing the error.
  67. virtual sys::MemoryBlock
  68. allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes,
  69. const sys::MemoryBlock *const NearBlock,
  70. unsigned Flags, std::error_code &EC) = 0;
  71. /// This method sets the protection flags for a block of memory to the state
  72. /// specified by \p Flags. The behavior is not specified if the memory was
  73. /// not allocated using the allocateMappedMemory method.
  74. /// \p Block describes the memory block to be protected.
  75. /// \p Flags specifies the new protection state to be assigned to the block.
  76. ///
  77. /// If \p Flags is MF_WRITE, the actual behavior varies with the operating
  78. /// system (i.e. MF_READ | MF_WRITE on Windows) and the target architecture
  79. /// (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
  80. ///
  81. /// \r error_success if the function was successful, or an error_code
  82. /// describing the failure if an error occurred.
  83. virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
  84. unsigned Flags) = 0;
  85. /// This method releases a block of memory that was allocated with the
  86. /// allocateMappedMemory method. It should not be used to release any memory
  87. /// block allocated any other way.
  88. /// \p Block describes the memory to be released.
  89. ///
  90. /// \r error_success if the function was successful, or an error_code
  91. /// describing the failure if an error occurred.
  92. virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M) = 0;
  93. virtual ~MemoryMapper();
  94. };
  95. /// Creates a SectionMemoryManager instance with \p MM as the associated
  96. /// memory mapper. If \p MM is nullptr then a default memory mapper is used
  97. /// that directly calls into the operating system.
  98. SectionMemoryManager(MemoryMapper *MM = nullptr);
  99. SectionMemoryManager(const SectionMemoryManager &) = delete;
  100. void operator=(const SectionMemoryManager &) = delete;
  101. ~SectionMemoryManager() override;
  102. /// Allocates a memory block of (at least) the given size suitable for
  103. /// executable code.
  104. ///
  105. /// The value of \p Alignment must be a power of two. If \p Alignment is zero
  106. /// a default alignment of 16 will be used.
  107. uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  108. unsigned SectionID,
  109. StringRef SectionName) override;
  110. /// Allocates a memory block of (at least) the given size suitable for
  111. /// executable code.
  112. ///
  113. /// The value of \p Alignment must be a power of two. If \p Alignment is zero
  114. /// a default alignment of 16 will be used.
  115. uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  116. unsigned SectionID, StringRef SectionName,
  117. bool isReadOnly) override;
  118. /// Update section-specific memory permissions and other attributes.
  119. ///
  120. /// This method is called when object loading is complete and section page
  121. /// permissions can be applied. It is up to the memory manager implementation
  122. /// to decide whether or not to act on this method. The memory manager will
  123. /// typically allocate all sections as read-write and then apply specific
  124. /// permissions when this method is called. Code sections cannot be executed
  125. /// until this function has been called. In addition, any cache coherency
  126. /// operations needed to reliably use the memory are also performed.
  127. ///
  128. /// \returns true if an error occurred, false otherwise.
  129. bool finalizeMemory(std::string *ErrMsg = nullptr) override;
  130. /// Invalidate instruction cache for code sections.
  131. ///
  132. /// Some platforms with separate data cache and instruction cache require
  133. /// explicit cache flush, otherwise JIT code manipulations (like resolved
  134. /// relocations) will get to the data cache but not to the instruction cache.
  135. ///
  136. /// This method is called from finalizeMemory.
  137. virtual void invalidateInstructionCache();
  138. private:
  139. struct FreeMemBlock {
  140. // The actual block of free memory
  141. sys::MemoryBlock Free;
  142. // If there is a pending allocation from the same reservation right before
  143. // this block, store it's index in PendingMem, to be able to update the
  144. // pending region if part of this block is allocated, rather than having to
  145. // create a new one
  146. unsigned PendingPrefixIndex;
  147. };
  148. struct MemoryGroup {
  149. // PendingMem contains all blocks of memory (subblocks of AllocatedMem)
  150. // which have not yet had their permissions applied, but have been given
  151. // out to the user. FreeMem contains all block of memory, which have
  152. // neither had their permissions applied, nor been given out to the user.
  153. SmallVector<sys::MemoryBlock, 16> PendingMem;
  154. SmallVector<FreeMemBlock, 16> FreeMem;
  155. // All memory blocks that have been requested from the system
  156. SmallVector<sys::MemoryBlock, 16> AllocatedMem;
  157. sys::MemoryBlock Near;
  158. };
  159. uint8_t *allocateSection(AllocationPurpose Purpose, uintptr_t Size,
  160. unsigned Alignment);
  161. std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
  162. unsigned Permissions);
  163. void anchor() override;
  164. MemoryGroup CodeMem;
  165. MemoryGroup RWDataMem;
  166. MemoryGroup RODataMem;
  167. MemoryMapper &MMapper;
  168. };
  169. } // end namespace llvm
  170. #endif // LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H