From 24ae5d704ee6b01d61c148dedc45e31acafbc571 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 26 Aug 2019 18:57:45 +0100 Subject: [PATCH 1/4] darwin: override zero/non zeroed pages to tag them. --- src/pal/pal_apple.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index cc9a96d..d2d6049 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -22,6 +22,56 @@ namespace snmalloc * should add any required features. */ static constexpr uint64_t pal_features = PALBSD::pal_features; + + // OS specific function for zeroing memory with ID 241. + template + void zero(void* p, size_t size) + { + if (page_aligned || bits::is_aligned_block(p, size)) + { + assert(bits::is_aligned_block(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 ID 241. + template + 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 + * 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. + */ + int pal_anon_id = 241 << 24; }; } // namespace snmalloc #endif From c881429e2ec8f0a9d3389ffc3254241ca03a168d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 27 Aug 2019 10:03:06 +0000 Subject: [PATCH 2/4] Using the macro instead --- src/pal/pal_apple.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index d2d6049..b82b6d4 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -3,6 +3,8 @@ #ifdef __APPLE__ # include "pal_bsd.h" +# include + namespace snmalloc { /** @@ -23,7 +25,7 @@ namespace snmalloc */ static constexpr uint64_t pal_features = PALBSD::pal_features; - // OS specific function for zeroing memory with ID 241. + // OS specific function for zeroing memory with ID 241. template void zero(void* p, size_t size) { @@ -45,7 +47,7 @@ namespace snmalloc bzero(p, size); } - // Reserve memory with ID 241. + // Reserve memory with ID 241. template void* reserve(const size_t* size) { @@ -62,16 +64,18 @@ namespace snmalloc return p; } + private: /** * Anonymous page tag ID * - * Darwin platform allows to gives an ID to anonymous pages - * 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. + * 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. */ - int pal_anon_id = 241 << 24; + int pal_anon_id = VM_MAKE_TAG(241); }; } // namespace snmalloc #endif From eed50baf8e9af15220cbb0607680cd2d5a01b48b Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 27 Aug 2019 10:35:34 +0000 Subject: [PATCH 3/4] Little tweaks --- src/pal/pal_apple.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index b82b6d4..a0b2bb8 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -25,7 +25,12 @@ namespace snmalloc */ static constexpr uint64_t pal_features = PALBSD::pal_features; - // OS specific function for zeroing memory with ID 241. + /** + * OS specific function for zeroing memory with the Apple application + * tag id. + * + * See comment below. + */ template void zero(void* p, size_t size) { @@ -47,7 +52,11 @@ namespace snmalloc bzero(p, size); } - // Reserve memory with ID 241. + /** + * Reserve memory with the Apple application tag id. + * + * See comment below. + */ template void* reserve(const size_t* size) { @@ -75,7 +84,7 @@ namespace snmalloc * (e.g. LLVM sanitizers has 99) so we can monitor their states * via vmmap for instance. */ - int pal_anon_id = VM_MAKE_TAG(241); + static constexpr int pal_anon_id = VM_MAKE_TAG(241); }; } // namespace snmalloc #endif From 281a88d404edc47a9879c99d095ac7bc68cf23c6 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 27 Aug 2019 12:52:52 +0000 Subject: [PATCH 4/4] Templatize the Tag ID value --- src/pal/pal.h | 2 +- src/pal/pal_apple.h | 8 +++----- src/pal/pal_consts.h | 5 +++++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/pal/pal.h b/src/pal/pal.h index 5a866a6..8b967b1 100644 --- a/src/pal/pal.h +++ b/src/pal/pal.h @@ -27,7 +27,7 @@ namespace snmalloc # if defined(_WIN32) PALWindows; # elif defined(__APPLE__) - PALApple; + PALApple<>; # elif defined(__linux__) PALLinux; # elif defined(FreeBSD_KERNEL) diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index a0b2bb8..72c3feb 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -9,11 +9,9 @@ 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 + template + class PALApple : public PALBSD> { public: /** @@ -84,7 +82,7 @@ namespace snmalloc * (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(241); + static constexpr int pal_anon_id = VM_MAKE_TAG(PALAnonID); }; } // namespace snmalloc #endif diff --git a/src/pal/pal_consts.h b/src/pal/pal_consts.h index b80c543..0261155 100644 --- a/src/pal/pal_consts.h +++ b/src/pal/pal_consts.h @@ -48,4 +48,9 @@ namespace snmalloc */ YesZero }; + + /** + * Default Tag ID for the Apple class + */ + static const int PALAnonDefaultID = 241; } // namespace snmalloc