forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSCompatEmscripten.cpp
More file actions
288 lines (228 loc) · 6.61 KB
/
OSCompatEmscripten.cpp
File metadata and controls
288 lines (228 loc) · 6.61 KB
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
288
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef __EMSCRIPTEN__
#include "hermes/Support/ErrorHandling.h"
#include "hermes/Support/OSCompat.h"
#include <cassert>
#include <vector>
#include <signal.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
#include "llvh/Support/raw_ostream.h"
namespace hermes {
namespace oscompat {
#ifndef NDEBUG
static size_t testPgSz = 0;
void set_test_page_size(size_t pageSz) {
testPgSz = pageSz;
}
void reset_test_page_size() {
testPgSz = 0;
}
#endif
static inline size_t page_size_real() {
return getpagesize();
}
size_t page_size() {
#ifndef NDEBUG
if (testPgSz != 0) {
return testPgSz;
}
#endif
return page_size_real();
}
#ifndef NDEBUG
static constexpr size_t unsetVMAllocLimit = std::numeric_limits<size_t>::max();
static size_t totalVMAllocLimit = unsetVMAllocLimit;
void set_test_vm_allocate_limit(size_t totSz) {
totalVMAllocLimit = totSz;
}
void unset_test_vm_allocate_limit() {
totalVMAllocLimit = unsetVMAllocLimit;
}
#endif // !NDEBUG
static llvh::ErrorOr<void *> vm_allocate_impl(size_t sz) {
#ifndef NDEBUG
if (LLVM_UNLIKELY(sz > totalVMAllocLimit)) {
return make_error_code(OOMError::TestVMLimitReached);
} else if (LLVM_UNLIKELY(totalVMAllocLimit != unsetVMAllocLimit)) {
totalVMAllocLimit -= sz;
}
#endif // !NDEBUG
void *result = mmap(
nullptr, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (result == MAP_FAILED) {
// Since mmap is a POSIX API, even on MacOS, errno should use the POSIX
// generic_category.
return std::error_code(errno, std::generic_category());
}
return result;
}
static char *alignAlloc(void *p, size_t alignment) {
return reinterpret_cast<char *>(
llvh::alignTo(reinterpret_cast<uintptr_t>(p), alignment));
}
llvh::ErrorOr<void *> vm_allocate(size_t sz) {
assert(sz % page_size() == 0);
#ifndef NDEBUG
if (testPgSz != 0 && testPgSz > static_cast<size_t>(page_size_real())) {
return vm_allocate_aligned(sz, testPgSz);
}
#endif // !NDEBUG
return vm_allocate_impl(sz);
}
llvh::ErrorOr<void *> vm_allocate_aligned(size_t sz, size_t alignment) {
assert(sz > 0 && sz % page_size() == 0);
assert(alignment > 0 && alignment % page_size() == 0);
// Opportunistically allocate without alignment constraint,
// and see if the memory happens to be aligned.
// While this may be unlikely on the first allocation request,
// subsequent allocation requests have a good chance.
auto result = vm_allocate_impl(sz);
if (!result) {
return result;
}
void *mem = *result;
if (mem == alignAlloc(mem, alignment)) {
return mem;
}
// Free the oppotunistic allocation.
oscompat::vm_free(mem, sz);
// This time, allocate a larger section to ensure that it contains
// a subsection that satisfies the request.
// Use *real* page size here since that's what vm_allocate_impl guarantees.
const size_t excessSize = sz + alignment - page_size_real();
result = vm_allocate_impl(excessSize);
if (!result)
return result;
void *raw = *result;
char *aligned = alignAlloc(raw, alignment);
size_t excessAtFront = aligned - static_cast<char *>(raw);
size_t excessAtBack = excessSize - excessAtFront - sz;
if (excessAtFront)
oscompat::vm_free(raw, excessAtFront);
if (excessAtBack)
oscompat::vm_free(aligned + sz, excessAtBack);
return aligned;
}
void vm_free(void *p, size_t sz) {
auto ret = munmap(p, sz);
assert(!ret && "Failed to free memory region.");
(void)ret;
#ifndef NDEBUG
if (LLVM_UNLIKELY(totalVMAllocLimit != unsetVMAllocLimit) && p) {
totalVMAllocLimit += sz;
}
#endif
}
void vm_free_aligned(void *p, size_t sz) {
vm_free(p, sz);
}
void vm_hugepage(void *p, size_t sz) {
assert(
reinterpret_cast<uintptr_t>(p) % page_size() == 0 &&
"Precondition: pointer is page-aligned.");
}
void vm_unused(void *p, size_t sz) {
#ifndef NDEBUG
const size_t PS = page_size();
assert(
reinterpret_cast<intptr_t>(p) % PS == 0 &&
"Precondition: pointer is page-aligned.");
#endif
}
void vm_prefetch(void *p, size_t sz) {
assert(
reinterpret_cast<intptr_t>(p) % page_size() == 0 &&
"Precondition: pointer is page-aligned.");
}
void vm_name(void *p, size_t sz, const char *name) {}
bool vm_protect(void *p, size_t sz, ProtectMode mode) {
auto prot = PROT_NONE;
if (mode == ProtectMode::ReadWrite) {
prot = PROT_WRITE | PROT_READ;
}
int err = mprotect(p, sz, prot);
return err != -1;
}
bool vm_madvise(void *p, size_t sz, MAdvice advice) {
#ifndef NDEBUG
const size_t PS = page_size();
assert(
reinterpret_cast<intptr_t>(p) % PS == 0 &&
"Precondition: pointer is page-aligned.");
#endif
return true;
}
llvh::ErrorOr<size_t> vm_footprint(char *start, char *end) {
return std::error_code(errno, std::generic_category());
}
int pages_in_ram(const void *p, size_t sz, llvh::SmallVectorImpl<int> *runs) {
return -1;
}
uint64_t peak_rss() {
return 0;
}
uint64_t current_rss() {
return 0;
}
uint64_t current_private_dirty() {
return 0;
}
std::vector<std::string> get_vm_protect_modes(const void *p, size_t sz) {
return std::vector<std::string>{};
}
bool num_context_switches(long &voluntary, long &involuntary) {
voluntary = involuntary = -1;
return false;
}
uint64_t thread_id() {
return 0;
}
// Platform-specific implementations of thread_cpu_time
std::chrono::microseconds thread_cpu_time() {
using namespace std::chrono;
struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
return microseconds::max();
}
microseconds::rep total = 0;
total += ts.tv_nsec / 1000;
total += ts.tv_sec * 1000000;
return microseconds(total);
}
// Platform-specific implementations of thread_page_fault_count
bool thread_page_fault_count(int64_t *outMinorFaults, int64_t *outMajorFaults) {
return false;
}
std::string thread_name() {
return "";
}
std::vector<bool> sched_getaffinity() {
// Not yet supported.
return std::vector<bool>();
}
int sched_getcpu() {
// Not yet supported.
return -1;
}
bool set_env(const char *name, const char *value) {
// Enforce the contract of this function that value must not be empty
assert(*value != '\0' && "value cannot be empty string");
return setenv(name, value, 1) == 0;
}
bool unset_env(const char *name) {
return unsetenv(name) == 0;
}
/*static*/
void *SigAltStackLeakSuppressor::stackRoot_{nullptr};
SigAltStackLeakSuppressor::~SigAltStackLeakSuppressor() {}
} // namespace oscompat
} // namespace hermes
#endif // __EMSCRIPTEN__