Merge pull request #92 from devnexen/pal_apple_anon_mon

darwin: override zero/non zeroed pages to tag them.
This commit is contained in:
Matthew Parkinson
2019-08-27 14:43:42 +01:00
committed by GitHub
3 changed files with 71 additions and 5 deletions

View File

@@ -27,7 +27,7 @@ namespace snmalloc
# if defined(_WIN32)
PALWindows;
# elif defined(__APPLE__)
PALApple;
PALApple<>;
# elif defined(__linux__)
PALLinux;
# elif defined(FreeBSD_KERNEL)

View File

@@ -3,15 +3,15 @@
#ifdef __APPLE__
# include "pal_bsd.h"
# include <mach/vm_statistics.h>
namespace snmalloc
{
/**
* PAL implementation for Apple systems (macOS, iOS, watchOS, tvOS...).
*
* XNU behaves exactly like a generic BSD platform but this class exists
* as a place to add XNU-specific behaviour later, if required.
*/
class PALApple : public PALBSD<PALApple>
template<int PALAnonID = PALAnonDefaultID>
class PALApple : public PALBSD<PALApple<>>
{
public:
/**
@@ -22,6 +22,67 @@ namespace snmalloc
* should add any required features.
*/
static constexpr uint64_t pal_features = PALBSD::pal_features;
/**
* OS specific function for zeroing memory with the Apple application
* tag id.
*
* See comment below.
*/
template<bool page_aligned = false>
void zero(void* p, size_t size)
{
if (page_aligned || bits::is_aligned_block<OS_PAGE_SIZE>(p, size))
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
void* r = mmap(
p,
size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
pal_anon_id,
0);
if (r != MAP_FAILED)
return;
}
bzero(p, size);
}
/**
* Reserve memory with the Apple application tag id.
*
* See comment below.
*/
template<bool committed>
void* reserve(const size_t* size)
{
void* p = mmap(
nullptr,
*size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
pal_anon_id,
0);
if (p == MAP_FAILED)
error("Out of memory");
return p;
}
private:
/**
* Anonymous page tag ID
*
* Darwin platform allows to gives an ID to anonymous pages via
* the VM_MAKE_TAG's macro, from 240 up to 255 are guaranteed
* to be free of usage, however eventually a lower could be taken
* (e.g. LLVM sanitizers has 99) so we can monitor their states
* via vmmap for instance.
*/
static constexpr int pal_anon_id = VM_MAKE_TAG(PALAnonID);
};
} // namespace snmalloc
#endif

View File

@@ -48,4 +48,9 @@ namespace snmalloc
*/
YesZero
};
/**
* Default Tag ID for the Apple class
*/
static const int PALAnonDefaultID = 241;
} // namespace snmalloc