Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
287 lines (257 loc) · 8.74 KB

File metadata and controls

287 lines (257 loc) · 8.74 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "base/memory.h"
#ifndef _WIN32
#include <sys/mman.h>
#include <unistd.h>
#include <cstdio>
#else
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#ifndef NOMINMAX
#define NOMINMAX 1
#endif
#include <windows.h>
#endif
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <new>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/base/config.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h"
#include "absl/numeric/bits.h"
#include "absl/synchronization/mutex.h"
#include "internal/no_destructor.h"
namespace cel {
namespace {
uintptr_t AlignUp(uintptr_t size, size_t align) {
ABSL_ASSERT(size != 0);
ABSL_ASSERT(absl::has_single_bit(align)); // Assert aligned to power of 2.
#if ABSL_HAVE_BUILTIN(__builtin_align_up)
return __builtin_align_up(size, align);
#else
return (size + static_cast<uintptr_t>(align) - uintptr_t{1}) &
~(static_cast<uintptr_t>(align) - uintptr_t{1});
#endif
}
template <typename T>
T* AlignUp(T* pointer, size_t align) {
return reinterpret_cast<T*>(
AlignUp(reinterpret_cast<uintptr_t>(pointer), align));
}
struct ArenaBlock final {
// The base pointer of the virtual memory, always points to the start of a
// page.
uint8_t* begin;
// The end pointer of the virtual memory, it's 1 past the last byte of the
// page(s).
uint8_t* end;
// The pointer to the first byte that we have not yet allocated.
uint8_t* current;
size_t remaining() const { return static_cast<size_t>(end - current); }
// Aligns the current pointer to `align`.
ArenaBlock& Align(size_t align) {
current = std::min(end, AlignUp(current, align));
return *this;
}
// Allocate `size` bytes from this block. This causes the current pointer to
// advance `size` bytes.
uint8_t* Allocate(size_t size) {
uint8_t* pointer = current;
current += size;
ABSL_ASSERT(current <= end);
return pointer;
}
size_t capacity() const { return static_cast<size_t>(end - begin); }
};
// Allocate a block of virtual memory from the kernel. `size` must be a multiple
// of `GetArenaPageSize()`. `hint` is a suggestion to the kernel of where we
// would like the virtual memory to be placed.
std::optional<ArenaBlock> ArenaBlockAllocate(size_t size,
void* hint = nullptr) {
void* pointer;
#ifndef _WIN32
pointer = mmap(hint, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ABSL_PREDICT_FALSE(pointer == MAP_FAILED)) {
return std::nullopt;
}
#else
pointer = VirtualAlloc(hint, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (ABSL_PREDICT_FALSE(pointer == nullptr)) {
if (hint == nullptr) {
return std::nullopt;
}
// Try again, without the hint.
pointer =
VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (pointer == nullptr) {
return std::nullopt;
}
}
#endif
ANNOTATE_MEMORY_IS_UNINITIALIZED(pointer, size);
return ArenaBlock{static_cast<uint8_t*>(pointer),
static_cast<uint8_t*>(pointer) + size,
static_cast<uint8_t*>(pointer)};
}
// Free the block of virtual memory with the kernel.
void ArenaBlockFree(void* pointer, size_t size) {
#ifndef _WIN32
if (ABSL_PREDICT_FALSE(munmap(pointer, size))) {
// If this happens its likely a bug and its probably corruption. Just bail.
std::perror("cel: failed to unmap pages from memory");
std::fflush(stderr);
std::abort();
}
#else
static_cast<void>(size);
if (ABSL_PREDICT_FALSE(!VirtualFree(pointer, 0, MEM_RELEASE))) {
// TODO(issues/5): print the error
std::abort();
}
#endif
}
class DefaultArenaMemoryManager final : public ArenaMemoryManager {
public:
~DefaultArenaMemoryManager() override {
absl::MutexLock lock(&mutex_);
for (const auto& owned : owned_) {
(*owned.second)(owned.first);
}
for (auto& block : blocks_) {
ArenaBlockFree(block.begin, block.capacity());
}
}
private:
void* Allocate(size_t size, size_t align) override {
auto page_size = base_internal::GetPageSize();
if (align > page_size) {
// Just, no. We refuse anything that requests alignment over the system
// page size.
return nullptr;
}
absl::MutexLock lock(&mutex_);
bool bridge_gap = false;
if (ABSL_PREDICT_FALSE(blocks_.empty() ||
blocks_.back().Align(align).remaining() == 0)) {
// Currently no allocated blocks or the allocation alignment is large
// enough that we cannot use any of the last block. Just allocate a block
// large enough.
auto maybe_block = ArenaBlockAllocate(AlignUp(size, page_size));
if (!maybe_block.has_value()) {
return nullptr;
}
blocks_.push_back(std::move(maybe_block).value());
} else {
// blocks_.back() was aligned above.
auto& last_block = blocks_.back();
size_t remaining = last_block.remaining();
if (ABSL_PREDICT_FALSE(remaining < size)) {
auto maybe_block =
ArenaBlockAllocate(AlignUp(size, page_size), last_block.end);
if (!maybe_block.has_value()) {
return nullptr;
}
bridge_gap = last_block.end == maybe_block.value().begin;
blocks_.push_back(std::move(maybe_block).value());
}
}
if (ABSL_PREDICT_FALSE(bridge_gap)) {
// The last block did not have enough to fit the requested size, so we had
// to allocate a new block. However the alignment was low enough and the
// kernel gave us the page immediately after the last. Therefore we can
// span the allocation across both blocks.
auto& second_last_block = blocks_[blocks_.size() - 2];
size_t remaining = second_last_block.remaining();
void* pointer = second_last_block.Allocate(remaining);
blocks_.back().Allocate(size - remaining);
return pointer;
}
return blocks_.back().Allocate(size);
}
void OwnDestructor(void* pointer, void (*destruct)(void*)) override {
absl::MutexLock lock(&mutex_);
owned_.emplace_back(pointer, destruct);
}
absl::Mutex mutex_;
std::vector<ArenaBlock> blocks_ ABSL_GUARDED_BY(mutex_);
std::vector<std::pair<void*, void (*)(void*)>> owned_ ABSL_GUARDED_BY(mutex_);
// TODO(issues/5): we could use a priority queue to keep track of any
// unallocated space at the end blocks.
};
} // namespace
class GlobalMemoryManager final : public MemoryManager {
public:
GlobalMemoryManager() : MemoryManager(false) {}
private:
// Never actually called by `MemoryManager`.
void* Allocate(size_t size, size_t align) override {
static_cast<void>(size);
static_cast<void>(align);
ABSL_UNREACHABLE();
return nullptr;
}
// Never actually called by `MemoryManager`.
void OwnDestructor(void* pointer, void (*destructor)(void*)) override {
static_cast<void>(pointer);
static_cast<void>(destructor);
ABSL_UNREACHABLE();
}
};
namespace base_internal {
// Returns the platforms page size. When requesting vitual memory from the
// kernel, typically the size requested must be a multiple of the page size.
size_t GetPageSize() {
static const size_t page_size = []() -> size_t {
#ifndef _WIN32
auto value = sysconf(_SC_PAGESIZE);
if (ABSL_PREDICT_FALSE(value == -1)) {
// This should not happen, if it does bail. There is no other way to
// determine the page size.
std::perror("cel: failed to determine system page size");
std::fflush(stderr);
std::abort();
}
return static_cast<size_t>(value);
#else
SYSTEM_INFO system_info;
SecureZeroMemory(&system_info, sizeof(system_info));
GetSystemInfo(&system_info);
return static_cast<size_t>(system_info.dwPageSize);
#endif
}();
return page_size;
}
} // namespace base_internal
MemoryManager& MemoryManager::Global() {
static internal::NoDestructor<GlobalMemoryManager> instance;
return *instance;
}
std::unique_ptr<ArenaMemoryManager> ArenaMemoryManager::Default() {
return std::make_unique<DefaultArenaMemoryManager>();
}
} // namespace cel
Morty Proxy This is a proxified and sanitized view of the page, visit original site.