From cf9b6290c7c9104deaab477f7a5f074d7ee30e63 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Fri, 4 Mar 2022 11:29:11 +0000 Subject: [PATCH] =?UTF-8?q?Follow-up=20on=20#469=20for=20Linux=20this=20ti?= =?UTF-8?q?me,=20overriding=20read=20only=20pages=20cod=E2=80=A6=20(#471)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Follow-up on #469 for Linux this time, overriding read only pages code path too. --- src/pal/pal_linux.h | 48 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/pal/pal_linux.h b/src/pal/pal_linux.h index 139a6af..aa28ded 100644 --- a/src/pal/pal_linux.h +++ b/src/pal/pal_linux.h @@ -34,6 +34,14 @@ namespace snmalloc */ static constexpr int default_mmap_flags = MAP_NORESERVE; + static void* reserve(size_t size) noexcept + { + void* p = PALPOSIX::reserve(size); + if (p) + madvise(p, size, MADV_DONTDUMP); + return p; + } + /** * OS specific function for zeroing memory. * @@ -68,20 +76,40 @@ namespace snmalloc { SNMALLOC_ASSERT(is_aligned_block(p, size)); + // Fill memory so that when we switch the pages back on we don't make + // assumptions on the content. + if constexpr (DEBUG) + memset(p, 0x5a, size); + + madvise(p, size, MADV_DONTDUMP); + madvise(p, size, MADV_FREE); + if constexpr (PalEnforceAccess) { - // Fill memory so that when we switch the pages back on we don't make - // assumptions on the content. - if constexpr (DEBUG) - memset(p, 0x5a, size); - - madvise(p, size, MADV_FREE); mprotect(p, size, PROT_NONE); } - else - { - madvise(p, size, MADV_FREE); - } + } + + /** + * Notify platform that we will be using these pages for reading. + * + * This is used only for pages full of zeroes and so we exclude them from + * core dumps. + */ + static void notify_using_readonly(void* p, size_t size) noexcept + { + PALPOSIX::notify_using_readonly(p, size); + madvise(p, size, MADV_DONTDUMP); + } + + /** + * Notify platform that we will be using these pages. + */ + template + static void notify_using(void* p, size_t size) noexcept + { + PALPOSIX::notify_using(p, size); + madvise(p, size, MADV_DODUMP); } static uint64_t get_entropy64()