public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@gnu.org>
To: libc-alpha@sourceware.org,
	Wilco Dijkstra <wilco.dijkstra@arm.com>,
	Cupertino Miranda <cupertino.miranda@oracle.com>
Subject: segfault after reallocation (Was: malloc: Change mmap chunk layout)
Date: Mon, 28 Sep 2026 01:32:19 +0200	[thread overview]
Message-ID: <armngyyT1otFqpSq@end> (raw)

Hello,

In Debian GNU/Hurd, starting with glibc 2.43, we see odd perfectly
reproducible segfaults in clang++, moc and others. What we observe is
some pointers unexpectedly getting NULL. I bisected that into

commit 614cfd0f8a2820aed54f9745077c7da0e6643bac
Author: Wilco Dijkstra <wilco.dijkstra@arm.com>
Date:   Fri Aug 8 14:11:13 2025 +0000

    malloc: Change mmap chunk layout

    Change the mmap chunk layout to be identical to a normal chunk.  This makes it
    safe for tcache to hold mmap chunks and simplifies size calculations in
    memsize and musable.  Add mmap_base() and mmap_size() macros to simplify code.

    Reviewed-by: Cupertino Miranda <cupertino.miranda@oracle.com>

Running with MALLOC_MMAP_MAX_=0 does not reproduce the issue. I tracked
the behavior down to a crash after a reallocation for mmap-backed
memory, and noticed something odd: in __libc_realloc, in the mmap case,
after failing to use mremap_chunk, we have

  if (chunk_is_mmapped (oldp))
    {
      [...]
      memcpy (newmem, oldmem, oldsize - CHUNK_HDR_SZ);
      munmap_chunk (oldp);
      return newmem;

Shouldn't this be

  if (chunk_is_mmapped (oldp))
    {
      [...]
      memcpy (newmem, oldmem, memsize (oldp));
      munmap_chunk (oldp);
      return newmem;

like in the other places of the file? Doing this change does make sense
to me and does fix the segfaults for me, see the proposed patch below.

Samuel


commit 7b031195e20d539f61e6465d843485b839375b0f
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Mon Sep 28 01:23:15 2026 +0200

    realloc: Fix mmap non-mremap reallocation case
    
    This case was missing copying SIZE_SZ bytes from the old allocation.
    
    Fixes commit 614cfd0f8a2820aed54f9745077c7da0e6643bac
    ("malloc: Change mmap chunk layout"), which went into glibc 2.43.

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 5fc35a14f4..30800203e7 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3131,7 +3131,7 @@ __libc_realloc (void *oldmem, size_t bytes)
       if (newmem == NULL)
         return NULL;              /* propagate failure */
 
-      memcpy (newmem, oldmem, oldsize - CHUNK_HDR_SZ);
+      memcpy (newmem, oldmem, memsize (oldp));
       munmap_chunk (oldp);
       return newmem;
     }

             reply	other threads:[~2026-09-27 23:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-27 23:32 Samuel Thibault [this message]
2026-09-28 13:03 ` Wilco Dijkstra
2026-09-28 18:59   ` Samuel Thibault
2026-09-28 16:43 ` Wilco Dijkstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=armngyyT1otFqpSq@end \
    --to=samuel.thibault@gnu.org \
    --cc=cupertino.miranda@oracle.com \
    --cc=libc-alpha@sourceware.org \
    --cc=wilco.dijkstra@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).