POSIX PAL: reset errno on failing mmap()s for zeroing

We try, if the region to be zeroed is sufficiently aligned, to use mmap to swap
out pages for zeros.
This commit is contained in:
Nathaniel Filardo
2021-01-14 16:32:32 +00:00
committed by Matthew Parkinson
parent 59edf294d0
commit 960733099b

View File

@@ -4,6 +4,7 @@
#if defined(BACKTRACE_HEADER)
# include BACKTRACE_HEADER
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -89,6 +90,22 @@ namespace snmalloc
static const int fd = T::anonymous_memory_fd;
};
/**
* A RAII class to capture and restore errno
*/
class KeepErrno
{
decltype(errno) cached_errno;
public:
KeepErrno() : cached_errno(errno) {}
~KeepErrno()
{
errno = cached_errno;
}
};
public:
/**
* Bitmap of PalFeatures flags indicating the optional features that this
@@ -186,6 +203,16 @@ namespace snmalloc
if (page_aligned || is_aligned_block<OS::page_size>(p, size))
{
SNMALLOC_ASSERT(is_aligned_block<OS::page_size>(p, size));
/*
* If mmap fails, we're going to fall back to zeroing the memory
* ourselves, which is not stellar, but correct. However, mmap() will
* have has left errno nonzero in an effort to explain its MAP_FAILED
* result. Capture its current value and restore it at the end of this
* block.
*/
auto hold = KeepErrno();
void* r = mmap(
p,
size,