LLVM  16.0.0git
MemoryMapper.h
Go to the documentation of this file.
1 //===- MemoryMapper.h - Cross-process memory mapper -------------*- 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 // Cross-process (and in-process) memory mapping and transfer
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
14 #define LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
15 
17 #include "llvm/Support/Process.h"
18 
19 #include <mutex>
20 
21 namespace llvm {
22 namespace orc {
23 
24 /// Manages mapping, content transfer and protections for JIT memory
25 class MemoryMapper {
26 public:
27  /// Represents a single allocation containing multiple segments and
28  /// initialization and deinitialization actions
29  struct AllocInfo {
30  struct SegInfo {
32  const char *WorkingMem;
33  size_t ContentSize;
34  size_t ZeroFillSize;
35  unsigned Prot;
36  };
37 
39  std::vector<SegInfo> Segments;
41  };
42 
44 
45  // Page size of the target process
46  virtual unsigned int getPageSize() = 0;
47 
48  /// Reserves address space in executor process
49  virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
50 
51  /// Provides working memory
52  virtual char *prepare(ExecutorAddr Addr, size_t ContentSize) = 0;
53 
55 
56  /// Ensures executor memory is synchronized with working copy memory, sends
57  /// functions to be called after initilization and before deinitialization and
58  /// applies memory protections
59  /// Returns a unique address identifying the allocation. This address should
60  /// be passed to deinitialize to run deallocation actions (and reset
61  /// permissions where possible).
62  virtual void initialize(AllocInfo &AI,
63  OnInitializedFunction OnInitialized) = 0;
64 
66 
67  /// Runs previously specified deinitialization actions
68  /// Executor addresses returned by initialize should be passed
69  virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
70  OnDeinitializedFunction OnDeInitialized) = 0;
71 
73 
74  /// Release address space acquired through reserve()
75  virtual void release(ArrayRef<ExecutorAddr> Reservations,
76  OnReleasedFunction OnRelease) = 0;
77 
78  virtual ~MemoryMapper();
79 };
80 
82 public:
83  InProcessMemoryMapper(size_t PageSize);
84 
86 
87  unsigned int getPageSize() override { return PageSize; }
88 
89  void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
90 
91  void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
92 
93  char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
94 
95  void deinitialize(ArrayRef<ExecutorAddr> Allocations,
96  OnDeinitializedFunction OnDeInitialized) override;
97 
98  void release(ArrayRef<ExecutorAddr> Reservations,
99  OnReleasedFunction OnRelease) override;
100 
101  ~InProcessMemoryMapper() override;
102 
103 private:
104  struct Allocation {
105  size_t Size;
106  std::vector<shared::WrapperFunctionCall> DeinitializationActions;
107  };
108  using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
109 
110  struct Reservation {
111  size_t Size;
112  std::vector<ExecutorAddr> Allocations;
113  };
114  using ReservationMap = DenseMap<void *, Reservation>;
115 
116  std::mutex Mutex;
117  ReservationMap Reservations;
118  AllocationMap Allocations;
119 
120  size_t PageSize;
121 };
122 
123 class SharedMemoryMapper final : public MemoryMapper {
124 public:
125  struct SymbolAddrs {
131  };
132 
134  size_t PageSize);
135 
138 
139  unsigned int getPageSize() override { return PageSize; }
140 
141  void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
142 
143  char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
144 
145  void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
146 
147  void deinitialize(ArrayRef<ExecutorAddr> Allocations,
148  OnDeinitializedFunction OnDeInitialized) override;
149 
150  void release(ArrayRef<ExecutorAddr> Reservations,
151  OnReleasedFunction OnRelease) override;
152 
153  ~SharedMemoryMapper() override;
154 
155 private:
156  struct Reservation {
157  void *LocalAddr;
158  size_t Size;
159  };
160 
161  ExecutorProcessControl &EPC;
162  SymbolAddrs SAs;
163 
164  std::mutex Mutex;
165 
166  std::map<ExecutorAddr, Reservation> Reservations;
167 
168  size_t PageSize;
169 };
170 
171 } // namespace orc
172 } // end namespace llvm
173 
174 #endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
llvm::orc::SharedMemoryMapper::SymbolAddrs
Definition: MemoryMapper.h:125
llvm::orc::SharedMemoryMapper::getPageSize
unsigned int getPageSize() override
Definition: MemoryMapper.h:139
llvm::orc::InProcessMemoryMapper::release
void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease) override
Release address space acquired through reserve()
Definition: MemoryMapper.cpp:135
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::SharedMemoryMapper::SymbolAddrs::Deinitialize
ExecutorAddr Deinitialize
Definition: MemoryMapper.h:129
llvm::unique_function
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
llvm::orc::SharedMemoryMapper::SymbolAddrs::Instance
ExecutorAddr Instance
Definition: MemoryMapper.h:126
llvm::orc::MemoryMapper::getPageSize
virtual unsigned int getPageSize()=0
llvm::orc::InProcessMemoryMapper::Create
static Expected< std::unique_ptr< InProcessMemoryMapper > > Create()
Definition: MemoryMapper.cpp:33
llvm::orc::InProcessMemoryMapper::reserve
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override
Reserves address space in executor process.
Definition: MemoryMapper.cpp:40
llvm::orc::MemoryMapper::~MemoryMapper
virtual ~MemoryMapper()
Definition: MemoryMapper.cpp:27
llvm::orc::MemoryMapper::reserve
virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved)=0
Reserves address space in executor process.
llvm::orc::ExecutorProcessControl
ExecutorProcessControl supports interaction with a JIT target process.
Definition: ExecutorProcessControl.h:38
llvm::orc::MemoryMapper::AllocInfo::MappingBase
ExecutorAddr MappingBase
Definition: MemoryMapper.h:38
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::orc::MemoryMapper::release
virtual void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease)=0
Release address space acquired through reserve()
llvm::orc::MemoryMapper::AllocInfo::SegInfo
Definition: MemoryMapper.h:30
Process.h
llvm::orc::SharedMemoryMapper::SymbolAddrs::Reserve
ExecutorAddr Reserve
Definition: MemoryMapper.h:127
llvm::orc::SharedMemoryMapper::Create
static Expected< std::unique_ptr< SharedMemoryMapper > > Create(ExecutorProcessControl &EPC, SymbolAddrs SAs)
Definition: MemoryMapper.cpp:200
llvm::orc::InProcessMemoryMapper::getPageSize
unsigned int getPageSize() override
Definition: MemoryMapper.h:87
llvm::sys::SmartMutex< false >
llvm::orc::MemoryMapper::AllocInfo::SegInfo::Offset
ExecutorAddrDiff Offset
Definition: MemoryMapper.h:31
llvm::orc::SharedMemoryMapper::reserve
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override
Reserves address space in executor process.
Definition: MemoryMapper.cpp:214
llvm::orc::SharedMemoryMapper::SymbolAddrs::Initialize
ExecutorAddr Initialize
Definition: MemoryMapper.h:128
llvm::orc::SharedMemoryMapper::SymbolAddrs::Release
ExecutorAddr Release
Definition: MemoryMapper.h:130
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:78
llvm::orc::InProcessMemoryMapper
Definition: MemoryMapper.h:81
llvm::DenseMap< ExecutorAddr, Allocation >
llvm::orc::InProcessMemoryMapper::initialize
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
Definition: MemoryMapper.cpp:62
llvm::orc::MemoryMapper::deinitialize
virtual void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized)=0
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
llvm::orc::MemoryMapper::AllocInfo::SegInfo::ZeroFillSize
size_t ZeroFillSize
Definition: MemoryMapper.h:34
llvm::orc::SharedMemoryMapper::release
void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease) override
Release address space acquired through reserve()
Definition: MemoryMapper.cpp:364
llvm::orc::SharedMemoryMapper::deinitialize
void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized) override
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
Definition: MemoryMapper.cpp:346
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::orc::shared::AllocActions
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
Definition: AllocationActions.h:44
llvm::orc::SharedMemoryMapper
Definition: MemoryMapper.h:123
llvm::orc::SharedMemoryMapper::initialize
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
Definition: MemoryMapper.cpp:303
llvm::orc::SharedMemoryMapper::SharedMemoryMapper
SharedMemoryMapper(ExecutorProcessControl &EPC, SymbolAddrs SAs, size_t PageSize)
Definition: MemoryMapper.cpp:191
llvm::orc::InProcessMemoryMapper::~InProcessMemoryMapper
~InProcessMemoryMapper() override
Definition: MemoryMapper.cpp:172
llvm::orc::MemoryMapper::AllocInfo::SegInfo::ContentSize
size_t ContentSize
Definition: MemoryMapper.h:33
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::orc::MemoryMapper
Manages mapping, content transfer and protections for JIT memory.
Definition: MemoryMapper.h:25
llvm::orc::InProcessMemoryMapper::prepare
char * prepare(ExecutorAddr Addr, size_t ContentSize) override
Provides working memory.
Definition: MemoryMapper.cpp:58
llvm::orc::MemoryMapper::AllocInfo::Actions
shared::AllocActions Actions
Definition: MemoryMapper.h:40
llvm::orc::SharedMemoryMapper::prepare
char * prepare(ExecutorAddr Addr, size_t ContentSize) override
Provides working memory.
Definition: MemoryMapper.cpp:293
llvm::orc::MemoryMapper::prepare
virtual char * prepare(ExecutorAddr Addr, size_t ContentSize)=0
Provides working memory.
Core.h
llvm::orc::MemoryMapper::initialize
virtual void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized)=0
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
llvm::orc::MemoryMapper::AllocInfo::Segments
std::vector< SegInfo > Segments
Definition: MemoryMapper.h:39
llvm::orc::MemoryMapper::OnReservedFunction
unique_function< void(Expected< ExecutorAddrRange >)> OnReservedFunction
Definition: MemoryMapper.h:43
llvm::orc::MemoryMapper::OnReleasedFunction
unique_function< void(Error)> OnReleasedFunction
Definition: MemoryMapper.h:72
llvm::orc::MemoryMapper::OnDeinitializedFunction
unique_function< void(Error)> OnDeinitializedFunction
Definition: MemoryMapper.h:65
llvm::orc::MemoryMapper::AllocInfo::SegInfo::WorkingMem
const char * WorkingMem
Definition: MemoryMapper.h:32
llvm::orc::MemoryMapper::AllocInfo
Represents a single allocation containing multiple segments and initialization and deinitialization a...
Definition: MemoryMapper.h:29
llvm::orc::InProcessMemoryMapper::deinitialize
void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized) override
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
Definition: MemoryMapper.cpp:105
llvm::orc::MemoryMapper::AllocInfo::SegInfo::Prot
unsigned Prot
Definition: MemoryMapper.h:35
llvm::orc::SharedMemoryMapper::~SharedMemoryMapper
~SharedMemoryMapper() override
Definition: MemoryMapper.cpp:413
llvm::orc::InProcessMemoryMapper::InProcessMemoryMapper
InProcessMemoryMapper(size_t PageSize)
Definition: MemoryMapper.cpp:29
llvm::orc::MemoryMapper::OnInitializedFunction
unique_function< void(Expected< ExecutorAddr >)> OnInitializedFunction
Definition: MemoryMapper.h:54