From d13d810b662d1b8a84f495f581c57be78e480578 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 26 Nov 2019 18:02:59 +0000 Subject: [PATCH 1/4] Explicitly compute the size of two pointers Don't use bits::is64() when setting MIN_ALLOC_BITS --- src/mem/allocconfig.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mem/allocconfig.h b/src/mem/allocconfig.h index fd0edc2..6238af3 100644 --- a/src/mem/allocconfig.h +++ b/src/mem/allocconfig.h @@ -110,8 +110,9 @@ namespace snmalloc #endif // Minimum allocation size is space for two pointers. - static constexpr size_t MIN_ALLOC_BITS = bits::is64() ? 4 : 3; - static constexpr size_t MIN_ALLOC_SIZE = 1 << MIN_ALLOC_BITS; + static_assert(bits::next_pow2_const(sizeof(void*)) == sizeof(void*)); + static constexpr size_t MIN_ALLOC_SIZE = 2 * sizeof(void*); + static constexpr size_t MIN_ALLOC_BITS = bits::ctz_const(MIN_ALLOC_SIZE); // Slabs are 64 KiB unless constrained to 16 KiB. static constexpr size_t SLAB_BITS = ADDRESS_SPACE_CONSTRAINED ? 14 : 16; From f8115a81b19f0d98e6656bc44a9949a743832d72 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 26 Nov 2019 18:01:23 +0000 Subject: [PATCH 2/4] pagemap: cast uintptr_t to size_t before bit math Not strictly necessary, but makes the CHERI compiler happier to see that we're using a decidedly integral type rather than a possibly tagged quantity. --- src/mem/pagemap.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 8526c09..94f5c1c 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -191,7 +191,7 @@ namespace snmalloc return std::pair(nullptr, 0); shift -= BITS_PER_INDEX_LEVEL; - ix = (addr >> shift) & ENTRIES_MASK; + ix = (static_cast(addr) >> shift) & ENTRIES_MASK; e = &value->entries[ix]; if constexpr (INDEX_LEVELS == 1) @@ -211,7 +211,7 @@ namespace snmalloc return std::pair(nullptr, 0); shift -= BITS_FOR_LEAF; - ix = (addr >> shift) & LEAF_MASK; + ix = (static_cast(addr) >> shift) & LEAF_MASK; return std::pair(leaf, ix); } From 5ae8ecedeeceb3a9f5bfd0d07b1439ca4e201dfb Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 26 Nov 2019 18:13:10 +0000 Subject: [PATCH 3/4] metaslab: further use of pointer align funcs --- src/mem/metaslab.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 990a323..0f959cb 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -142,12 +142,12 @@ namespace snmalloc static Slab* get_slab(void* p) { - return pointer_cast(address_cast(p) & SLAB_MASK); + return pointer_align_down(p); } static bool is_short(Slab* p) { - return (address_cast(p) & SUPERSLAB_MASK) == address_cast(p); + return pointer_align_down(p) == p; } /** From 7afc6da566a8f3a65562f0a897a94d9195d44219 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 26 Nov 2019 18:13:22 +0000 Subject: [PATCH 4/4] pagemap: cast to size_t then do bit manip Makes the CHERI compiler happier --- src/mem/pagemap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 94f5c1c..ce2aa5e 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -387,7 +387,7 @@ namespace snmalloc */ size_t index_for_address(uintptr_t p) { - return reinterpret_cast((p >> SHIFT) & (OS_PAGE_SIZE - 1)); + return bits::align_down(static_cast(p) >> SHIFT, OS_PAGE_SIZE); } /**