Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expanding the page range id feature to e able to distinguish better #509

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/snmalloc/pal/pal_consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ namespace snmalloc
YesZero
};

enum StateMem
{
Unused,
Allocated,
Pagemap,
Metadata
};

/**
* Default Tag ID for the Apple class
*/
Expand Down
64 changes: 42 additions & 22 deletions src/snmalloc/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,54 @@ namespace snmalloc
*/
static constexpr int default_mmap_flags = MAP_NORESERVE;

static void* reserve(size_t size) noexcept
/**
* Not generalizing this handler purposely as in other platforms
* mechanisms could differ greatly.
*/
template<StateMem state_mem>
static void pageid(void* p, size_t size) noexcept
{
void* p = PALPOSIX<PALLinux>::reserve(size);
if (p)
{
madvise(p, size, MADV_DONTDUMP);
# ifdef SNMALLOC_PAGEID
# ifndef PR_SET_VMA
# define PR_SET_VMA 0x53564d41
# define PR_SET_VMA_ANON_NAME 0
# endif

/**
*
* If the kernel is set with CONFIG_ANON_VMA_NAME
* the reserved pages would appear as follow
*
* 7fa5f0ceb000-7fa5f0e00000 rw-p 00000000 00:00 0 [anon:snmalloc]
* 7fa5f0e00000-7fa5f1800000 rw-p 00000000 00:00 0 [anon:snmalloc]
*
*/

prctl(
PR_SET_VMA,
PR_SET_VMA_ANON_NAME,
(unsigned long)p,
size,
(unsigned long)"snmalloc");
/**
*
* If the kernel is set with CONFIG_ANON_VMA_NAME
* the reserved pages would appear as follow
*
* 7fa5f0ceb000-7fa5f0e00000 rw-p 00000000 00:00 0 [anon:snmalloc
* (<type>)] 7fa5f0e00000-7fa5f1800000 rw-p 00000000 00:00 0
* [anon:snmalloc (<type>)]
*
* The 80 buffer limit is specific to this syscall.
*/

char buf[80] = {0};
PALPOSIX<PALLinux>::pagetype<state_mem>(buf, sizeof(buf));

prctl(
PR_SET_VMA,
PR_SET_VMA_ANON_NAME,
(unsigned long)p,
size,
(unsigned long)buf);
# else
UNUSED(p);
UNUSED(size);
# endif
}

template<StateMem state_mem = Unused>
static void* reserve(size_t size) noexcept
{
void* p = PALPOSIX<PALLinux>::reserve<state_mem>(size);
if (p)
{
madvise(p, size, MADV_DONTDUMP);
pageid<state_mem>(p, size);
}
return p;
}
Expand Down Expand Up @@ -131,11 +150,12 @@ namespace snmalloc
/**
* Notify platform that we will be using these pages.
*/
template<ZeroMem zero_mem>
template<ZeroMem zero_mem, StateMem state_mem = Allocated>
static void notify_using(void* p, size_t size) noexcept
{
PALPOSIX<PALLinux>::notify_using<zero_mem>(p, size);
madvise(p, size, MADV_DODUMP);
pageid<state_mem>(p, size);
}

static uint64_t get_entropy64()
Expand Down
18 changes: 17 additions & 1 deletion src/snmalloc/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ namespace snmalloc
* are also zeroing the pages in which case we call the platform's `zero`
* function, or we have initially mapped the pages as PROT_NONE.
*/
template<ZeroMem zero_mem>
template<ZeroMem zero_mem, StateMem state_mem = Allocated>
static void notify_using(void* p, size_t size) noexcept
{
SNMALLOC_ASSERT(
Expand Down Expand Up @@ -316,6 +316,7 @@ namespace snmalloc
* POSIX does not define a portable interface for specifying alignment
* greater than a page.
*/
template<StateMem state_mem = Unused>
static void* reserve(size_t size) noexcept
{
// If enforcing access, map pages initially as None, and then
Expand Down Expand Up @@ -428,5 +429,20 @@ namespace snmalloc

error("Failed to get system randomness");
}

template<StateMem state_mem>
static void pagetype(char* buf, size_t len)
{
ssize_t sz;
if constexpr (state_mem == Unused)
sz = snprintf(buf, len, "snmalloc (Unused)");
else if constexpr (state_mem == Allocated)
sz = snprintf(buf, len, "snmalloc (Allocated)");
else if constexpr (state_mem == Pagemap)
sz = snprintf(buf, len, "snmalloc (Pagemap)");
else if constexpr (state_mem == Metadata)
sz = snprintf(buf, len, "snmalloc (Metadata)");
buf[sz] = 0;
}
};
} // namespace snmalloc
1 change: 1 addition & 0 deletions src/snmalloc/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ namespace snmalloc
}
# endif

template<StateMem state_mem = Unused>
static void* reserve(size_t size) noexcept
{
void* ret = VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
Expand Down