From 48f66cf4a22af3b380d4c049f79fb7e820eba3d3 Mon Sep 17 00:00:00 2001 From: Shirui Cheng Date: Thu, 18 Jul 2024 15:36:08 -0700 Subject: [PATCH] add a size check when declare a stack array to be less than 2048 bytes --- include/jemalloc/internal/jemalloc_internal_types.h | 8 ++++++-- src/ctl.c | 4 ++-- src/stats.c | 2 +- test/unit/hash.c | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/jemalloc/internal/jemalloc_internal_types.h b/include/jemalloc/internal/jemalloc_internal_types.h index 4ab5a0cf..6a81f3cd 100644 --- a/include/jemalloc/internal/jemalloc_internal_types.h +++ b/include/jemalloc/internal/jemalloc_internal_types.h @@ -135,10 +135,14 @@ typedef enum malloc_init_e malloc_init_t; # include # endif # endif -# define VARIABLE_ARRAY(type, name, count) \ +# define VARIABLE_ARRAY_UNSAFE(type, name, count) \ type *name = alloca(sizeof(type) * (count)) #else -# define VARIABLE_ARRAY(type, name, count) type name[(count)] +# define VARIABLE_ARRAY_UNSAFE(type, name, count) type name[(count)] #endif +#define VARIABLE_ARRAY_SIZE_MAX 2048 +#define VARIABLE_ARRAY(type, name, count) \ + assert(sizeof(type) * (count) <= VARIABLE_ARRAY_SIZE_MAX); \ + VARIABLE_ARRAY_UNSAFE(type, name, count) #endif /* JEMALLOC_INTERNAL_TYPES_H */ diff --git a/src/ctl.c b/src/ctl.c index 62589d77..ebe5c61c 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -1379,7 +1379,7 @@ ctl_refresh(tsdn_t *tsdn) { const unsigned narenas = ctl_arenas->narenas; assert(narenas > 0); ctl_arena_t *ctl_sarena = arenas_i(MALLCTL_ARENAS_ALL); - VARIABLE_ARRAY(arena_t *, tarenas, narenas); + VARIABLE_ARRAY_UNSAFE(arena_t *, tarenas, narenas); /* * Clear sum stats, since they will be merged into by @@ -2726,7 +2726,7 @@ arena_i_decay(tsdn_t *tsdn, unsigned arena_ind, bool all) { */ if (arena_ind == MALLCTL_ARENAS_ALL || arena_ind == narenas) { unsigned i; - VARIABLE_ARRAY(arena_t *, tarenas, narenas); + VARIABLE_ARRAY_UNSAFE(arena_t *, tarenas, narenas); for (i = 0; i < narenas; i++) { tarenas[i] = arena_get(tsdn, i, false); diff --git a/src/stats.c b/src/stats.c index 726007f5..fbfacabf 100644 --- a/src/stats.c +++ b/src/stats.c @@ -1896,7 +1896,7 @@ stats_print_helper(emitter_t *emitter, bool merged, bool destroyed, size_t mib[3]; size_t miblen = sizeof(mib) / sizeof(size_t); size_t sz; - VARIABLE_ARRAY(bool, initialized, narenas); + VARIABLE_ARRAY_UNSAFE(bool, initialized, narenas); bool destroyed_initialized; unsigned i, ninitialized; diff --git a/test/unit/hash.c b/test/unit/hash.c index 49f08238..17c66ec6 100644 --- a/test/unit/hash.c +++ b/test/unit/hash.c @@ -61,8 +61,8 @@ static void hash_variant_verify_key(hash_variant_t variant, uint8_t *key) { const int hashbytes = hash_variant_bits(variant) / 8; const int hashes_size = hashbytes * 256; - VARIABLE_ARRAY(uint8_t, hashes, hashes_size); - VARIABLE_ARRAY(uint8_t, final, hashbytes); + VARIABLE_ARRAY_UNSAFE(uint8_t, hashes, hashes_size); + VARIABLE_ARRAY_UNSAFE(uint8_t, final, hashbytes); unsigned i; uint32_t computed, expected;