SectionMemoryManager.h 8.6 KB

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