From d09b784e395554cb67ec91e70544a052fe60a276 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 30 Oct 2024 14:57:23 -0400 Subject: sanity: Don't mark memory uninit in sanitize_{alloc,free}() We might want to change the size of an allocated region without changing which bytes are initialized. --- src/alloc.c | 6 ++++-- src/sanity.h | 14 +++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/alloc.c b/src/alloc.c index 79e4ce7..ef9f6ab 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -228,6 +228,7 @@ void arena_free(struct arena *arena, void *ptr) { union chunk *chunk = ptr; chunk_set_next(arena, chunk, arena->chunks); arena->chunks = chunk; + sanitize_uninit(chunk, arena->size); sanitize_free(chunk, arena->size); } @@ -334,15 +335,16 @@ void *varena_realloc(struct varena *varena, void *ptr, size_t old_count, size_t } size_t old_size = old_arena->size; - sanitize_alloc((char *)ptr + old_exact_size, old_size - old_exact_size); + sanitize_alloc(ptr, old_size); size_t new_size = new_arena->size; size_t min_size = new_size < old_size ? new_size : old_size; memcpy(ret, ptr, min_size); arena_free(old_arena, ptr); - sanitize_free((char *)ret + new_exact_size, new_size - new_exact_size); + sanitize_free(ret, new_size); + sanitize_alloc(ret, new_exact_size); return ret; } diff --git a/src/sanity.h b/src/sanity.h index 0b770cf..3f6020b 100644 --- a/src/sanity.h +++ b/src/sanity.h @@ -20,6 +20,11 @@ #define SANITIZE_CALL__(macro, ptr, size, ...) \ macro(ptr, size) +/** + * Squelch unused variable warnings when not sanitizing. + */ +#define sanitize_ignore(ptr, size) ((void)(ptr), (void)(size)) + #if __SANITIZE_ADDRESS__ # include @@ -38,8 +43,8 @@ #define sanitize_free(...) SANITIZE_CALL(__asan_poison_memory_region, __VA_ARGS__) #else -# define sanitize_alloc sanitize_uninit -# define sanitize_free sanitize_uninit +# define sanitize_alloc(...) SANITIZE_CALL(sanitize_ignore, __VA_ARGS__) +# define sanitize_free(...) SANITIZE_CALL(sanitize_ignore, __VA_ARGS__) #endif #if __SANITIZE_MEMORY__ @@ -64,11 +69,6 @@ # define sanitize_uninit(...) SANITIZE_CALL(sanitize_ignore, __VA_ARGS__) #endif -/** - * Squelch unused variable warnings when not sanitizing. - */ -#define sanitize_ignore(ptr, size) ((void)(ptr), (void)(size)) - /** * Initialize a variable, unless sanitizers would detect uninitialized uses. */ -- cgit v1.2.3