From patchwork Sun Sep 27 23:32:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 144749 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 071AF4BA23CE for ; Sun, 27 Sep 2026 23:32:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 071AF4BA23CE Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=BEs6O9Bu X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 6501E4BA7994 for ; Sun, 27 Sep 2026 23:32:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6501E4BA7994 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6501E4BA7994 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790551943; cv=none; b=Pdq8arTFtkeU72dLzx53HaYmuPxdZP3YiuJVdoV9lz9RjCx/sHSB4tfIkriw8hVVcBObw1M8kNWwZmRZy96HMOJ89GcyYOJoFGbpijid3FcGyQnFIvRaRTyU7MzAPNlZpjPs/Aq3lLib4CmUYYM9v8vOqPg7ZGhkc51/XvqZRD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790551943; c=relaxed/simple; bh=EqVfLzvyxiHgf2JZCHpZSjhK0d6t0OJ+oSiiKw60dvQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=x+JqePdjMLAUI2+pAX6jxd3zLObue1GLN4ZCgihzVxCpqBKG6q3scHoTuKjdlecIts+0GFfo1x/t2eTTLses0MoPO93WTwJyL9wzv7AI3pExo8BYMgF9ALeZpIAPMExlO9Z+2EleipZlZPk1+QW6hjseY75lecosJHniiavJEDg= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=BEs6O9Bu DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6501E4BA7994 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xAyM1-0002XA-LT; Sun, 27 Sep 2026 19:32:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Subject:To:From:Date:in-reply-to: references; bh=468UNdVqHvmEXF19Ec9Rk3O6QdL8zFlOb4bUahCt1VI=; b=BEs6O9BuFddWWw RigpUTX10wRNAdaOVBmsr/Iw/W7MVPwIOJvTgCh/8Tz2NnI4HJbNUncS4jYuSrIXnKZfrMzrclNd1 MFD6sFtWYk82S+wBLfER0kO53Zs+n5SCg0tlV4Flv+YuBMBw7Oc6OAuGL6hoxX/goMohyqgi3cLeC kVGqeG/Osz8aG0K//Ax5LOBn4NK5lOsIR92c3wQvNHftcnODdP5FvGRPigM3jReL0IPU8ufAtTaUf ITZvtrS58Szu60tsgHQffbRJtrhJqfpCfJtuQm7NPVPhENQ0tZxFFuHTO66gAu86Wx5897Ixh0Zm5 IKx9ikFM4REJZ+YGlNsw==; Date: Mon, 28 Sep 2026 01:32:19 +0200 From: Samuel Thibault To: libc-alpha@sourceware.org, Wilco Dijkstra , Cupertino Miranda Subject: segfault after reallocation (Was: malloc: Change mmap chunk layout) Message-ID: MIME-Version: 1.0 Content-Disposition: inline Organization: I am not organized X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org 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 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 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 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; }