diff options
Diffstat (limited to 'src/zmalloc.h')
-rw-r--r-- | src/zmalloc.h | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/zmalloc.h b/src/zmalloc.h index 8d4c980cc..1ff66a635 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -96,20 +96,23 @@ #define HAVE_DEFRAG #endif -__attribute__((malloc)) void *zmalloc(size_t size); -__attribute__((malloc)) void *zcalloc(size_t size); -__attribute__((malloc)) __attribute((alloc_size(1,2))) void *zcalloc_num(size_t num, size_t size); -__attribute__((alloc_size(2))) void *zrealloc(void *ptr, size_t size); -__attribute__((malloc)) void *ztrymalloc(size_t size); -__attribute__((malloc)) void *ztrycalloc(size_t size); -__attribute__((alloc_size(2))) void *ztryrealloc(void *ptr, size_t size); +/* 'noinline' attribute is intended to prevent the `-Wstringop-overread` warning + * when using gcc-12 later with LTO enabled. It may be removed once the + * bug[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503] is fixed. */ +__attribute__((malloc,alloc_size(1),noinline)) void *zmalloc(size_t size); +__attribute__((malloc,alloc_size(1),noinline)) void *zcalloc(size_t size); +__attribute__((malloc,alloc_size(1,2),noinline)) void *zcalloc_num(size_t num, size_t size); +__attribute__((alloc_size(2),noinline)) void *zrealloc(void *ptr, size_t size); +__attribute__((malloc,alloc_size(1),noinline)) void *ztrymalloc(size_t size); +__attribute__((malloc,alloc_size(1),noinline)) void *ztrycalloc(size_t size); +__attribute__((alloc_size(2),noinline)) void *ztryrealloc(void *ptr, size_t size); void zfree(void *ptr); -__attribute__((malloc)) __attribute__((alloc_size(1))) void *zmalloc_usable(size_t size, size_t *usable); -__attribute__((malloc)) __attribute__((alloc_size(1))) void *zcalloc_usable(size_t size, size_t *usable); -__attribute__((alloc_size(2))) void *zrealloc_usable(void *ptr, size_t size, size_t *usable); -__attribute__((malloc)) __attribute__((alloc_size(1))) void *ztrymalloc_usable(size_t size, size_t *usable); -__attribute__((malloc)) __attribute__((alloc_size(1))) void *ztrycalloc_usable(size_t size, size_t *usable); -__attribute__((alloc_size(2))) void *ztryrealloc_usable(void *ptr, size_t size, size_t *usable); +void *zmalloc_usable(size_t size, size_t *usable); +void *zcalloc_usable(size_t size, size_t *usable); +void *zrealloc_usable(void *ptr, size_t size, size_t *usable); +void *ztrymalloc_usable(size_t size, size_t *usable); +void *ztrycalloc_usable(size_t size, size_t *usable); +void *ztryrealloc_usable(void *ptr, size_t size, size_t *usable); void zfree_usable(void *ptr, size_t *usable); __attribute__((malloc)) char *zstrdup(const char *s); size_t zmalloc_used_memory(void); @@ -117,7 +120,7 @@ void zmalloc_set_oom_handler(void (*oom_handler)(size_t)); size_t zmalloc_get_rss(void); int zmalloc_get_allocator_info(size_t *allocated, size_t *active, size_t *resident); void set_jemalloc_bg_thread(int enable); -int jemalloc_purge(); +int jemalloc_purge(void); size_t zmalloc_get_private_dirty(long pid); size_t zmalloc_get_smap_bytes_by_field(char *field, long pid); size_t zmalloc_get_memory_size(void); @@ -133,7 +136,22 @@ __attribute__((malloc)) void *zmalloc_no_tcache(size_t size); size_t zmalloc_size(void *ptr); size_t zmalloc_usable_size(void *ptr); #else +/* If we use 'zmalloc_usable_size()' to obtain additional available memory size + * and manipulate it, we need to call 'extend_to_usable()' afterwards to ensure + * the compiler recognizes this extra memory. However, if we use the pointer + * obtained from z[*]_usable() family functions, there is no need for this step. */ #define zmalloc_usable_size(p) zmalloc_size(p) + +/* derived from https://github.com/systemd/systemd/pull/25688 + * We use zmalloc_usable_size() everywhere to use memory blocks, but that is an abuse since the + * malloc_usable_size() isn't meant for this kind of use, it is for diagnostics only. That is also why the + * behavior is flaky when built with _FORTIFY_SOURCE, the compiler can sense that we reach outside + * the allocated block and SIGABRT. + * We use a dummy allocator function to tell the compiler that the new size of ptr is newsize. + * The implementation returns the pointer as is; the only reason for its existence is as a conduit for the + * alloc_size attribute. This cannot be a static inline because gcc then loses the attributes on the function. + * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 */ +__attribute__((alloc_size(2),noinline)) void *extend_to_usable(void *ptr, size_t size); #endif int get_proc_stat_ll(int i, long long *res); |