]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Cleanup macros, asserts and sysmalloc_mmap_fallback
authorWilliam Hunt <william.hunt@arm.com>
Fri, 3 Oct 2025 16:27:35 +0000 (16:27 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Fri, 3 Oct 2025 16:34:10 +0000 (16:34 +0000)
Refactor malloc.c to remove dead code, create macros to abstract duplicated
code, and cleanup sysmalloc_mmap_fallback to remove logic not related to the
mmap call.

Change the return type of mmap_base to uintptr_t since this allows using
operations on the return value, and avoids casting in both calls in
mremap_chunk and munmap_chunk.

Cleanup sysmalloc_mmap_fallback. Remove unused parameters nb, oldsize
and av. Remove redundant overflow check and instead use size_t for all
parameters except extra_flags to prevent overflows. Move logic not concerned
with the mmap call itself outside the function after both calls to
sysmalloc_mmap_fallback are made; this means move code for naming the VMA
and marking the arena being extended as non-contiguous to the calling code to
be handled in the case that the mmap is successful. Calculate the fallback
size from nb to avoid modifying size after it has been set for MORECORE.

Remove unused noncontiguous macro.

Remove redundant assert for checking unreachable option for global_max_fast.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>
malloc/malloc-check.c
malloc/malloc.c

index 40b215ea83f43fbc09dc5f303454ce516c9a6ae1..bfb88ca84effdeed9429a4715633ee72a2b9507e 100644 (file)
@@ -151,7 +151,7 @@ mem2chunk_check (void *mem, unsigned char **magic_p)
            offset != 0x200 && offset != 0x400 && offset != 0x800 && offset != 0x1000 &&
            offset < 0x2000) ||
           !chunk_is_mmapped (p) || prev_inuse (p) ||
-          ((((uintptr_t) mmap_base (p)) & page_mask) != 0) ||
+          (((mmap_base (p)) & page_mask) != 0) ||
           (mmap_size (p) & page_mask) != 0)
         return NULL;
 
index 802318d1436e20bec6cd56cdfba80b70b03a1739..f46cfd5eac74b7cc9f9284fd073a927dba038d05 100644 (file)
@@ -1502,10 +1502,10 @@ prev_size_mmap (mchunkptr p)
 }
 
 /* Return pointer to mmap base from a chunk with IS_MMAPPED set.  */
-static __always_inline void *
+static __always_inline uintptr_t
 mmap_base (mchunkptr p)
 {
-  return (char *) p - prev_size_mmap (p);
+  return (uintptr_t) p - prev_size_mmap (p);
 }
 
 /* Return total mmap size of a chunk with IS_MMAPPED set.  */
@@ -1809,7 +1809,6 @@ typedef struct malloc_chunk *mfastbinptr;
 #define NONCONTIGUOUS_BIT     (2U)
 
 #define contiguous(M)          (((M)->flags & NONCONTIGUOUS_BIT) == 0)
-#define noncontiguous(M)       (((M)->flags & NONCONTIGUOUS_BIT) != 0)
 #define set_noncontiguous(M)   ((M)->flags |= NONCONTIGUOUS_BIT)
 #define set_contiguous(M)      ((M)->flags &= ~NONCONTIGUOUS_BIT)
 
@@ -2324,9 +2323,6 @@ do_check_malloc_state (mstate av)
 
   /* properties of fastbins */
 
-  /* max_fast is in allowed range */
-  assert ((get_max_fast () & ~1) <= request2size (MAX_FAST_SIZE));
-
   max_fast_bin = fastbin_index (get_max_fast ());
 
   for (i = 0; i < NFASTBINS; ++i)
@@ -2492,24 +2488,15 @@ sysmalloc_mmap (INTERNAL_SIZE_T nb, size_t pagesize, int extra_flags)
    if MORECORE fails.
  */
 static void *
-sysmalloc_mmap_fallback (long int *s, INTERNAL_SIZE_T nb,
-                        INTERNAL_SIZE_T old_size, size_t minsize,
-                        size_t pagesize, int extra_flags, mstate av)
+sysmalloc_mmap_fallback (size_t *s, size_t size, size_t minsize,
+                         size_t pagesize, int extra_flags)
 {
-  long int size = *s;
-
-  /* Cannot merge with old top, so add its size back in */
-  if (contiguous (av))
-    size = ALIGN_UP (size + old_size, pagesize);
+  size = ALIGN_UP (size, pagesize);
 
   /* If we are relying on mmap as backup, then use larger units */
-  if ((unsigned long) (size) < minsize)
+  if (size < minsize)
     size = minsize;
 
-  /* Don't try if size wraps around 0 */
-  if ((unsigned long) (size) <= (unsigned long) (nb))
-    return MORECORE_FAILURE;
-
   char *mbrk = (char *) (MMAP (NULL, size,
                               mtag_mmap_flags | PROT_READ | PROT_WRITE,
                               extra_flags));
@@ -2519,13 +2506,6 @@ sysmalloc_mmap_fallback (long int *s, INTERNAL_SIZE_T nb,
   if (extra_flags == 0)
     madvise_thp (mbrk, size);
 
-  __set_vma_name (mbrk, size, " glibc: malloc");
-
-  /* Record that we no longer have a contiguous sbrk region.  After the first
-     time mmap is used as backup, we do not ever rely on contiguous space
-     since this could incorrectly bridge regions.  */
-  set_noncontiguous (av);
-
   *s = size;
   return mbrk;
 }
@@ -2537,7 +2517,7 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av)
   INTERNAL_SIZE_T old_size;       /* its size */
   char *old_end;                  /* its end address */
 
-  long size;                      /* arg to first MORECORE or mmap call */
+  size_t size;                      /* arg to first MORECORE or mmap call */
   char *brk;                      /* return value from MORECORE */
 
   long correction;                /* arg to 2nd MORECORE call */
@@ -2703,9 +2683,9 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av)
          below even if we cannot call MORECORE.
        */
 
-      if (size > 0)
+      if ((ssize_t) size > 0)
         {
-          brk = (char *) (MORECORE (size));
+          brk = (char *) (MORECORE ((long) size));
          if (brk != (char *) (MORECORE_FAILURE))
            madvise_thp (brk, size);
           LIBC_PROBE (memory_sbrk_more, 2, brk, size);
@@ -2722,16 +2702,25 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av)
              segregated mmap region.
            */
 
+         size_t fallback_size = nb + mp_.top_pad + MINSIZE;
          char *mbrk = MAP_FAILED;
          if (mp_.hp_pagesize > 0)
-           mbrk = sysmalloc_mmap_fallback (&size, nb, old_size,
-                                           mp_.hp_pagesize, mp_.hp_pagesize,
-                                           mp_.hp_flags, av);
+           mbrk = sysmalloc_mmap_fallback (&size, fallback_size,
+                                           mp_.hp_pagesize,
+                                           mp_.hp_pagesize, mp_.hp_flags);
          if (mbrk == MAP_FAILED)
-           mbrk = sysmalloc_mmap_fallback (&size, nb, old_size, MMAP_AS_MORECORE_SIZE,
-                                           pagesize, 0, av);
+           mbrk = sysmalloc_mmap_fallback (&size, fallback_size,
+                                           MMAP_AS_MORECORE_SIZE,
+                                           pagesize, 0);
          if (mbrk != MAP_FAILED)
            {
+             __set_vma_name (mbrk, fallback_size, " glibc: malloc");
+
+             /* Record that we no longer have a contiguous sbrk region.  After the first
+                time mmap is used as backup, we do not ever rely on contiguous space
+                since this could incorrectly bridge regions.  */
+             set_noncontiguous (av);
+
              /* We do not need, and cannot use, another sbrk call to find end */
              brk = mbrk;
              snd_brk = brk + size;
@@ -3024,7 +3013,7 @@ munmap_chunk (mchunkptr p)
   assert (chunk_is_mmapped (p));
 
   uintptr_t mem = (uintptr_t) chunk2mem (p);
-  uintptr_t block = (uintptr_t) mmap_base (p);
+  uintptr_t block = mmap_base (p);
   size_t total_size = mmap_size (p);
   /* Unfortunately we have to do the compilers job by hand here.  Normally
      we would test BLOCK and TOTAL-SIZE separately for compliance with the
@@ -3056,7 +3045,7 @@ mremap_chunk (mchunkptr p, size_t new_size)
 
   assert (chunk_is_mmapped (p));
 
-  uintptr_t block = (uintptr_t) mmap_base (p);
+  uintptr_t block = mmap_base (p);
   uintptr_t mem = (uintptr_t) chunk2mem(p);
   size_t total_size = mmap_size (p);
   if (__glibc_unlikely ((block | total_size) & (pagesize - 1)) != 0