From 79ad6630d354233e6b2eefb7901c0d39a0476a12 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Fri, 15 May 2020 19:27:32 +0000 Subject: [PATCH] alloc: eliminate external_pointer/address distinction Just always work with pointers using the functions defined in ds/address.h. This more obviously preserves provenance through the chain of reasoning. Note that there is still risk of malloc() being used as an amplification oracle on CHERI, but there's no additional risk from this change. Rename the external_address into external_pointer. --- src/mem/alloc.h | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 82005df..b008e61 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -394,7 +394,7 @@ namespace snmalloc } template - static address_t external_address(void* p) + static void* external_pointer(void* p) { #ifdef USE_MALLOC error("Unsupported"); @@ -423,12 +423,13 @@ namespace snmalloc return external_pointer(p, sc, slab_end); } - auto ss = address_cast(super); + auto ss = super; while (size > 64) { // This is a large alloc redirect. - ss = ss - (1ULL << (size - 64)); + ss = pointer_offset_signed( + ss, -(static_cast(1) << (size - 64))); size = ChunkMap::get(ss); } @@ -436,28 +437,22 @@ namespace snmalloc { if constexpr ((location == End) || (location == OnePastEnd)) // We don't know the End, so return MAX_PTR - return UINTPTR_MAX; + return pointer_offset(nullptr, UINTPTR_MAX); else // We don't know the Start, so return MIN_PTR - return 0; + return nullptr; } // This is a large alloc, mask off to the slab size. if constexpr (location == Start) return ss; else if constexpr (location == End) - return (ss + (1ULL << size) - 1ULL); + return pointer_offset(ss, (1ULL << size) - 1ULL); else - return (ss + (1ULL << size)); + return pointer_offset(ss, 1ULL << size); #endif } - template - static void* external_pointer(void* p) - { - return pointer_cast(external_address(p)); - } - static size_t alloc_size(const void* p) { // This must be called on an external pointer. @@ -824,24 +819,24 @@ namespace snmalloc } template - static uintptr_t + static void* external_pointer(void* p, sizeclass_t sizeclass, void* end_point) { size_t rsize = sizeclass_to_size(sizeclass); void* end_point_correction = location == End ? - (static_cast(end_point) - 1) : - (location == OnePastEnd ? end_point : - (static_cast(end_point) - rsize)); + pointer_offset_signed(end_point, -1) : + (location == OnePastEnd ? + end_point : + pointer_offset_signed(end_point, -static_cast(rsize))); - ptrdiff_t offset_from_end = - (static_cast(end_point) - 1) - static_cast(p); + size_t offset_from_end = + pointer_diff(p, pointer_offset_signed(end_point, -1)); - size_t end_to_end = - round_by_sizeclass(rsize, static_cast(offset_from_end)); + size_t end_to_end = round_by_sizeclass(rsize, offset_from_end); - return address_cast( - static_cast(end_point_correction) - end_to_end); + return pointer_offset_signed( + end_point_correction, -static_cast(end_to_end)); } void init_message_queue()