[PATCH v2] malloc: Only trim on free [BZ #34882]
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Fri Nov 6 17:52:24 GMT 2026
v2: Also add trim calls to realloc paths that can free large chunks
Malloc may free blocks internally - currently these behave like free().
This may result in trimming which updates internal state and can result
in malloc returning NULL without running out of memory. Remove calls to
_int_free_maybe_trim() and explicitly call it from _int_free_chunk()
and _int_realloc(). This fixes BZ #34882.
Passes regress, OK for commit?
---
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 73ab3a32372176e867f25c8eb5aef88f574d89ee..a1b27072a020ba9c71ca4c34f83ca793c610413e 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -821,7 +821,8 @@ typedef struct malloc_chunk* mchunkptr;
static void* _int_malloc(mstate, size_t);
static void _int_free_chunk (mstate, mchunkptr, INTERNAL_SIZE_T);
-static void _int_free_merge_chunk (mstate, mchunkptr, INTERNAL_SIZE_T);
+static INTERNAL_SIZE_T _int_free_merge_chunk (mstate, mchunkptr,
+ INTERNAL_SIZE_T);
static INTERNAL_SIZE_T _int_free_create_chunk (mstate,
mchunkptr, INTERNAL_SIZE_T,
mchunkptr, INTERNAL_SIZE_T);
@@ -3963,11 +3964,15 @@ _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size)
if (!chunk_is_mmapped(p)) {
if (SINGLE_THREAD_P)
- _int_free_merge_chunk (av, p, size);
+ {
+ size = _int_free_merge_chunk (av, p, size);
+ _int_free_maybe_trim (av, size);
+ }
else
{
__libc_lock_lock (av->mutex);
- _int_free_merge_chunk (av, p, size);
+ size = _int_free_merge_chunk (av, p, size);
+ _int_free_maybe_trim (av, size);
__libc_lock_unlock (av->mutex);
}
}
@@ -4001,7 +4006,7 @@ _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size)
/* Try to merge chunk P of SIZE bytes from locked arena AV with its neighbors.
Put the resulting chunk on the appropriate bin list. P must not be on a
bin list yet, and it can be in use. */
-static void
+static INTERNAL_SIZE_T
_int_free_merge_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size)
{
mchunkptr nextchunk = chunk_at_offset(p, size);
@@ -4040,8 +4045,7 @@ _int_free_merge_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size)
}
/* Write the chunk header, maybe after merging with the following chunk. */
- size = _int_free_create_chunk (av, p, size, nextchunk, nextsize);
- _int_free_maybe_trim (av, size);
+ return _int_free_create_chunk (av, p, size, nextchunk, nextsize);
}
/* Create a chunk at P of SIZE bytes, with SIZE potentially increased
@@ -4240,7 +4244,8 @@ _int_realloc (mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
{
void *oldmem = chunk2mem (oldp);
memcpy (newmem, oldmem, memsize (oldp));
- _int_free_merge_chunk (av, oldp, chunksize (oldp));
+ oldsize = _int_free_merge_chunk (av, oldp, chunksize (oldp));
+ _int_free_maybe_trim (av, oldsize);
check_inuse_chunk (av, newp);
return newmem;
}
@@ -4266,7 +4271,9 @@ _int_realloc (mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
(av != &main_arena ? NON_MAIN_ARENA : 0));
/* Mark remainder as inuse so free() won't complain */
set_inuse_bit_at_offset (remainder, remainder_size);
- _int_free_merge_chunk (av, remainder, chunksize (remainder));
+ oldsize = _int_free_merge_chunk (av, remainder, remainder_size);
+ /* Trim since the remainder can be very large. */
+ _int_free_maybe_trim (av, oldsize);
}
check_inuse_chunk (av, newp);
@@ -4337,7 +4344,6 @@ _int_memalign (mstate av, size_t alignment, size_t bytes)
set_head_size (p, nb);
size = _int_free_create_chunk (av, remainder, size - nb, nextchunk,
chunksize (nextchunk));
- _int_free_maybe_trim (av, size);
}
check_inuse_chunk (av, p);
More information about the Libc-alpha
mailing list