Allocated memory using MAP_NORESERVE (#1204)

In `mm.c`, memory mappings are now created using `MAP_NORESERVE`.
By default, the call attempts to allocate petabytes, failing eagerly on some systems.
With `MAP_NORESERVE`, the oversized allocation succeeds, but a segfault occurs if
there is not enough backing physical memory as execution progresses.
This commit is contained in:
Nandor Licker 2022-09-09 18:52:01 +03:00 committed by GitHub
parent cb755ee969
commit 81249667eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions

View File

@ -37,8 +37,12 @@ void mm_t::init(size_t sz, int wsz, int lsz) {
assert(wsz > 0 && lsz > 0 && (lsz & (lsz - 1)) == 0 && lsz % wsz == 0);
word_size = wsz;
line_size = lsz;
data = (uint8_t *)mmap(
NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
data = (uint8_t *)mmap(NULL,
sz,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS | MAP_NORESERVE,
-1,
0);
if (data == MAP_FAILED) {
std::perror("[mm_t] mmap for backing storage failed");
exit(-1);