From e5ca2eb27d6c599f1512f75c2daae6b93406893c Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Wed, 10 Jul 2024 09:30:33 -0400 Subject: [PATCH] jemalloc: Clamp in malloc_usable_size() only if a tag is present This behaviour should be safe and is useful for testing whether the bounds applied by jemalloc match what malloc_usable_size() reports. That is, one can validate bounds with a check like, assert(cheri_getlen(p) == malloc_usable_size(cheri_cleartag(p))); In particular, malloc_usable_size() will look up the usable size using allocator metadata, so it's handy to be able to compare that size with the capability bounds. --- contrib/jemalloc/src/jemalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/jemalloc/src/jemalloc.c b/contrib/jemalloc/src/jemalloc.c index 76bcfc93a148..f29802449fa6 100644 --- a/contrib/jemalloc/src/jemalloc.c +++ b/contrib/jemalloc/src/jemalloc.c @@ -3770,7 +3770,7 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) { ret = isalloc(tsdn, ptr); } #ifdef __CHERI_PURE_CAPABILITY__ - if (ret != 0) { + if (ret != 0 && cheri_gettag(ptr)) { ret = MIN(ret, cheri_getlen(ptr)); } #endif