1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
//===- MemoryMapper.cpp - Cross-process memory mapper ------------*- C++ -*-==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/MemoryMapper.h"
namespace llvm {
namespace orc {
MemoryMapper::~MemoryMapper() {}
void InProcessMemoryMapper::reserve(size_t NumBytes,
OnReservedFunction OnReserved) {
std::error_code EC;
auto MB = sys::Memory::allocateMappedMemory(
NumBytes, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
if (EC)
return OnReserved(errorCodeToError(EC));
{
std::lock_guard<std::mutex> Lock(Mutex);
Reservations[MB.base()].Size = MB.allocatedSize();
}
OnReserved(
ExecutorAddrRange(ExecutorAddr::fromPtr(MB.base()), MB.allocatedSize()));
}
char *InProcessMemoryMapper::prepare(ExecutorAddr Addr, size_t ContentSize) {
return Addr.toPtr<char *>();
}
void InProcessMemoryMapper::initialize(MemoryMapper::AllocInfo &AI,
OnInitializedFunction OnInitialized) {
ExecutorAddr MinAddr(~0ULL);
for (auto &Segment : AI.Segments) {
auto Base = AI.MappingBase + Segment.Offset;
auto Size = Segment.ContentSize + Segment.ZeroFillSize;
if (Base < MinAddr)
MinAddr = Base;
std::memset((Base + Segment.ContentSize).toPtr<void *>(), 0,
Segment.ZeroFillSize);
if (auto EC = sys::Memory::protectMappedMemory({Base.toPtr<void *>(), Size},
Segment.Prot)) {
return OnInitialized(errorCodeToError(EC));
}
if (Segment.Prot & sys::Memory::MF_EXEC)
sys::Memory::InvalidateInstructionCache(Base.toPtr<void *>(), Size);
}
auto DeinitializeActions = shared::runFinalizeActions(AI.Actions);
if (!DeinitializeActions)
return OnInitialized(DeinitializeActions.takeError());
{
std::lock_guard<std::mutex> Lock(Mutex);
Allocations[MinAddr].DeinitializationActions =
std::move(*DeinitializeActions);
Reservations[AI.MappingBase.toPtr<void *>()].Allocations.push_back(MinAddr);
}
OnInitialized(MinAddr);
}
void InProcessMemoryMapper::deinitialize(
ArrayRef<ExecutorAddr> Bases,
MemoryMapper::OnDeinitializedFunction OnDeinitialized) {
Error AllErr = Error::success();
{
std::lock_guard<std::mutex> Lock(Mutex);
for (auto Base : Bases) {
if (Error Err = shared::runDeallocActions(
Allocations[Base].DeinitializationActions)) {
AllErr = joinErrors(std::move(AllErr), std::move(Err));
}
Allocations.erase(Base);
}
}
OnDeinitialized(std::move(AllErr));
}
void InProcessMemoryMapper::release(ArrayRef<ExecutorAddr> Bases,
OnReleasedFunction OnReleased) {
Error Err = Error::success();
for (auto Base : Bases) {
std::vector<ExecutorAddr> AllocAddrs;
size_t Size;
{
std::lock_guard<std::mutex> Lock(Mutex);
auto &R = Reservations[Base.toPtr<void *>()];
Size = R.Size;
AllocAddrs.swap(R.Allocations);
}
// deinitialize sub allocations
std::promise<MSVCPError> P;
auto F = P.get_future();
deinitialize(AllocAddrs, [&](Error Err) { P.set_value(std::move(Err)); });
if (Error E = F.get()) {
Err = joinErrors(std::move(Err), std::move(E));
}
// free the memory
auto MB = sys::MemoryBlock(Base.toPtr<void *>(), Size);
auto EC = sys::Memory::releaseMappedMemory(MB);
if (EC) {
Err = joinErrors(std::move(Err), errorCodeToError(EC));
}
std::lock_guard<std::mutex> Lock(Mutex);
Reservations.erase(Base.toPtr<void *>());
}
OnReleased(std::move(Err));
}
InProcessMemoryMapper::~InProcessMemoryMapper() {
std::vector<ExecutorAddr> ReservationAddrs;
{
std::lock_guard<std::mutex> Lock(Mutex);
ReservationAddrs.reserve(Reservations.size());
for (const auto &R : Reservations) {
ReservationAddrs.push_back(ExecutorAddr::fromPtr(R.getFirst()));
}
}
std::promise<MSVCPError> P;
auto F = P.get_future();
release(ReservationAddrs, [&](Error Err) { P.set_value(std::move(Err)); });
cantFail(F.get());
}
} // namespace orc
} // namespace llvm
|