From 869acdbbabd3f751828b0a971e91fa8a745ead7b Mon Sep 17 00:00:00 2001 From: Peter Mitsis Date: Wed, 11 Dec 2024 14:00:29 -0800 Subject: [PATCH] kernel: Add CONFIG_MEM_SLAB_POINTER_VALIDATE Makes the validation of both allocated memory slab pointer and the memory slab pointer to free configurable. Signed-off-by: Peter Mitsis --- kernel/Kconfig | 7 +++++++ kernel/mem_slab.c | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/kernel/Kconfig b/kernel/Kconfig index 62713a5c9bb..361fb67f17e 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -677,6 +677,13 @@ config POLL concurrently, which can be either directly triggered or triggered by the availability of some kernel objects (semaphores and FIFOs). +config MEM_SLAB_POINTER_VALIDATE + bool "Validate the memory slab pointer when allocating or freeing" + default ASSERT + help + This enables additional runtime checks to validate the memory slab + pointer during when allocating or freeing a memory slab. + config MEM_SLAB_TRACE_MAX_UTILIZATION bool "Getting maximum slab utilization" help diff --git a/kernel/mem_slab.c b/kernel/mem_slab.c index 45ba08e27cb..7abb61af504 100644 --- a/kernel/mem_slab.c +++ b/kernel/mem_slab.c @@ -206,6 +206,10 @@ out: static bool slab_ptr_is_good(struct k_mem_slab *slab, const void *ptr) { + if (!IS_ENABLED(CONFIG_MEM_SLAB_POINTER_VALIDATE)) { + return true; + } + const char *p = ptr; ptrdiff_t offset = p - slab->buffer;