2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-29 13:38:26 +00:00

Use max_align_t for memory sizeinfo alignment on OpenBSD

On OpenBSD and more generally on platforms without either jemalloc or
malloc_(usable_)size, we need to increase the alignment for the memory
to sizeof(max_align_t) as with plain sizeof(void *), the compiled code
would be crashing when accessing the returned memory.
This commit is contained in:
Ondrej Sury 2021-07-13 12:35:52 +02:00 committed by Ondřej Surý
parent 97e1a1f929
commit 6eca4b402e

View File

@ -13,6 +13,8 @@
#if !defined(HAVE_JEMALLOC) #if !defined(HAVE_JEMALLOC)
#include <stddef.h>
#include <isc/util.h> #include <isc/util.h>
const char *malloc_conf = NULL; const char *malloc_conf = NULL;
@ -76,41 +78,58 @@ sallocx(void *ptr, int flags) {
#include <stdlib.h> #include <stdlib.h>
typedef union {
size_t size;
max_align_t __alignment;
} size_info;
static inline void * static inline void *
mallocx(size_t size, int flags) { mallocx(size_t size, int flags) {
void *ptr = NULL;
UNUSED(flags); UNUSED(flags);
size_t *__ptr = malloc(size + sizeof(size_t)); size_info *si = malloc(size + sizeof(*si));
REQUIRE(__ptr != NULL); REQUIRE(si != NULL);
__ptr[0] = size;
return (&__ptr[1]); si->size = size;
ptr = &si[1];
return (ptr);
} }
static inline void static inline void
sdallocx(void *ptr, size_t size, int flags) { sdallocx(void *ptr, size_t size, int flags) {
size_info *si = &(((size_info *)ptr)[-1]);
UNUSED(size); UNUSED(size);
UNUSED(flags); UNUSED(flags);
free(&((size_t *)ptr)[-1]); free(si);
} }
static inline size_t static inline size_t
sallocx(void *ptr, int flags) { sallocx(void *ptr, int flags) {
size_info *si = &(((size_info *)ptr)[-1]);
UNUSED(flags); UNUSED(flags);
return (((size_t *)ptr)[-1]); return (si[0].size);
} }
static inline void * static inline void *
rallocx(void *ptr, size_t size, int flags) { rallocx(void *ptr, size_t size, int flags) {
size_info *si = &(((size_info *)ptr)[-1]);
UNUSED(flags); UNUSED(flags);
size_t *__ptr = realloc(&((size_t *)ptr)[-1], size + sizeof(size_t)); si = realloc(si, size + sizeof(*si));
REQUIRE(__ptr != NULL); REQUIRE(si != NULL);
__ptr[0] = size;
return (&__ptr[1]); si->size = size;
ptr = &si[1];
return (ptr);
} }
#endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */ #endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */