From cc6a9775d6127dbd0bac568ff5e76f4ee9b0dfd1 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Wed, 12 Jun 2019 15:26:42 +0100 Subject: [PATCH] Made checks on client have own macro. --- src/mem/alloc.h | 8 ++++---- src/mem/allocconfig.h | 6 ++++++ src/mem/metaslab.h | 4 ++-- src/mem/slab.h | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/mem/alloc.h b/src/mem/alloc.h index ce6c494..703c163 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -469,7 +469,7 @@ namespace snmalloc return; } -# ifndef NDEBUG +# ifdef CHECK_CLIENT if (size > 64 || address_cast(super) != address_cast(p)) { error("Not deallocating start of an object"); @@ -817,7 +817,7 @@ namespace snmalloc { Superslab* super = Superslab::get(p); -#ifndef NDEBUG +#ifdef CHECK_CLIENT if (p->target_id() != super->get_allocator()->id()) error("Detected memory corruption. Potential use-after-free"); #endif @@ -1013,7 +1013,7 @@ namespace snmalloc void small_dealloc(Superslab* super, void* p, uint8_t sizeclass) { -#ifndef NDEBUG +#ifdef CHECK_CLIENT Slab* slab = Slab::get(p); if (!slab->is_start_of_object(super, p)) { @@ -1150,7 +1150,7 @@ namespace snmalloc stats().sizeclass_dealloc(sizeclass); bool was_full = slab->dealloc(p, large_allocator.memory_provider); -#ifndef NDEBUG +#ifdef CHECK_CLIENT if (!is_multiple_of_sizeclass( sizeclass_to_size(sizeclass), address_cast(slab) + SUPERSLAB_SIZE - address_cast(p))) diff --git a/src/mem/allocconfig.h b/src/mem/allocconfig.h index 3b2cb14..e1a3117 100644 --- a/src/mem/allocconfig.h +++ b/src/mem/allocconfig.h @@ -4,6 +4,12 @@ namespace snmalloc { +// The CHECK_CLIENT macro is used to turn on minimal checking of the client +// calling the API correctly. +#if !defined(NDEBUG) && !defined(CHECK_CLIENT) +# define CHECK_CLIENT +#endif + // 0 intermediate bits results in power of 2 small allocs. 1 intermediate // bit gives additional sizeclasses at the midpoint between each power of 2. // 2 intermediate bits gives 3 intermediate sizeclasses, etc. diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 068edb8..7bb2a32 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -99,7 +99,7 @@ namespace snmalloc /// simple corruptions. static void store_next(void* p, uint16_t head) { -#ifdef NDEBUG +#ifndef CHECK_CLIENT *static_cast(p) = head; #else *static_cast(p) = @@ -112,7 +112,7 @@ namespace snmalloc static uint16_t follow_next(void* node) { size_t next = *static_cast(node); -#ifndef NDEBUG +#ifdef CHECK_CLIENT if (((next ^ POISON) ^ (next << (bits::BITS - 16))) > 0xFFFF) error("Detected memory corruption. Use-after-free."); #endif diff --git a/src/mem/slab.h b/src/mem/slab.h index 67ff01b..0442740 100644 --- a/src/mem/slab.h +++ b/src/mem/slab.h @@ -101,7 +101,7 @@ namespace snmalloc bool was_full = meta.is_full(); -#ifndef NDEBUG +#ifdef CHECK_CLIENT if (meta.is_unused()) error("Detected potential double free."); #endif