NetBSD Problem Report #59062

From martin@aprisoft.de  Mon Feb 10 14:05:54 2025
Return-Path: <martin@aprisoft.de>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits)
	 client-signature RSA-PSS (2048 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id B575B1A923A
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 10 Feb 2025 14:05:54 +0000 (UTC)
Message-Id: <20250210140545.DC7CC5CC7DB@emmas.aprisoft.de>
Date: Mon, 10 Feb 2025 15:05:45 +0100 (CET)
From: martin@NetBSD.org
Reply-To: martin@NetBSD.org
To: gnats-bugs@NetBSD.org
Subject: jemalloc does not guarantee proper alignment
X-Send-Pr-Version: 3.95

>Number:         59062
>Category:       lib
>Synopsis:       jemalloc does not guarantee proper alignment
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    lib-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Feb 10 14:10:01 +0000 2025
>Closed-Date:    Sat Oct 04 16:15:00 +0000 2025
>Last-Modified:  Sat Oct 04 16:15:00 +0000 2025
>Originator:     Martin Husemann
>Release:        NetBSD 10.99.12
>Organization:
The NetBSD Foundation, Inc.
>Environment:
System: NetBSD seven-days-to-the-wolves.aprisoft.de 10.99.12 NetBSD 10.99.12 (GENERIC) #771: Thu Jan 30 09:04:03 CET 2025 martin@seven-days-to-the-wolves.aprisoft.de:/work/src/sys/arch/amd64/compile/GENERIC amd64
Architecture: x86_64
Machine: amd64
>Description:

It seems that jemalloc guarantees proper alignment for bigger allocations,
but for small ones it only provides 2^3 alignment - which might not be
good enough for some of our architectures.

See src/external/bsd/jemalloc/include/jemalloc/internal/size_classes.h

We ran into this with jemalloc 1.0 before and it is pretty clear that
it violates the C standard:

   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958
   https://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_075.html
   https://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_445.htm

We tried to catch this misconfiguration by a hack in gcc, which now is
ineffective and should be removed.

>How-To-Repeat:

Source code inspection

>Fix:

Noting that the relevant MALLOC_ABI_ALIGNMENT in our gcc configs
defaults to register size and only is overriden for arm we get a
simple #ifdef cascade.

The patch also removes the outdated local change in gcc.

Index: gpl3/gcc/dist/gcc/tree-ssa-ccp.cc
===================================================================
RCS file: /cvsroot/src/external/gpl3/gcc/dist/gcc/tree-ssa-ccp.cc,v
retrieving revision 1.4
diff -u -p -r1.4 tree-ssa-ccp.cc
--- gpl3/gcc/dist/gcc/tree-ssa-ccp.cc	1 Jul 2024 05:31:10 -0000	1.4
+++ gpl3/gcc/dist/gcc/tree-ssa-ccp.cc	7 Feb 2025 11:41:44 -0000
@@ -4643,45 +4643,3 @@ make_pass_post_ipa_warn (gcc::context *c
 {
   return new pass_post_ipa_warn (ctxt);
 }
-
-#if defined(__NetBSD__) && defined(NETBSD_NATIVE)
-/*
- * This is a big, ugly, temporary hack:
- *    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958
- * To make sure we have configured all our targets correctly, mimic the
- * #ifdef cascade from src/lib/libc/stdlib/jemalloc.c here and compile
- * time assert that the value matches gcc's MALLOC_ABI_ALIGNMENT here.
- */
-
-#if defined(__hppa__)
-#define	JEMALLOC_TINY_MIN_2POW	4
-#elif defined(__alpha__) || defined(__amd64__) || defined(__sparc64__)	\
-     ||	(defined(__arm__) && defined(__ARM_EABI__)) \
-     || defined(__ia64__) || defined(__powerpc__) \
-     || defined(__aarch64__) \
-     || ((defined(__mips__) || defined(__riscv__)) && defined(_LP64))
-#define	JEMALLOC_TINY_MIN_2POW	3
-#endif
-
-#ifndef JEMALLOC_TINY_MIN_2POW
-#define	JEMALLOC_TINY_MIN_2POW	2
-#endif
-
-/* make sure we test the (native) 64bit variant for targets supporting -m32 */
-#undef	TARGET_64BIT
-#ifdef _LP64
-#define	TARGET_64BIT	1
-#else
-#define	TARGET_64BIT	0
-#endif
-
-/* ARM has a non-constant MALLOC_ABI_ALIGNMENT since GCC 5.  */
-#if !defined(__arm__)
-#ifdef __CTASSERT
-__CTASSERT((8<<JEMALLOC_TINY_MIN_2POW) == MALLOC_ABI_ALIGNMENT);
-#else
-#error compiling on an older NetBSD version?
-#endif
-#endif
-
-#endif
Index: bsd/jemalloc/include/jemalloc/internal/size_classes.h
===================================================================
RCS file: /cvsroot/src/external/bsd/jemalloc/include/jemalloc/internal/size_classes.h,v
retrieving revision 1.4
diff -u -p -r1.4 size_classes.h
--- bsd/jemalloc/include/jemalloc/internal/size_classes.h	28 Mar 2019 11:56:56 -0000	1.4
+++ bsd/jemalloc/include/jemalloc/internal/size_classes.h	7 Feb 2025 11:43:14 -0000
@@ -35,7 +35,16 @@
  */

 #define LG_SIZE_CLASS_GROUP	2
+
+#ifdef __NetBSD__
+#if defined(_LP64) || defined(__arm__)
+#define LG_TINY_MIN		4
+#else
+#define LG_TINY_MIN		3
+#endif
+#else
 #define LG_TINY_MIN		3
+#endif

 #if (LG_SIZEOF_PTR == 2 && LG_TINY_MIN == 3 && LG_QUANTUM == 3 && LG_PAGE == 12)
 #define SIZE_CLASSES \

>Release-Note:

>Audit-Trail:
From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59062 CVS commit: src/external/gpl3/gcc/dist/gcc
Date: Thu, 6 Mar 2025 10:02:10 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Thu Mar  6 10:02:10 UTC 2025

 Modified Files:
 	src/external/gpl3/gcc/dist/gcc: tree-ssa-ccp.cc

 Log Message:
 Remove a (local) "temporary cludge" that has outlived its usefullness
 when we updated from jemalloc 1.0 to newer versions: remove a compile
 time test supposed to guarantee that gcc's MALLOC_ABI_ALIGNMENT is
 in sync with the minimum alignemnet guaranteed by jemalloc.
 Tangentialy related to (and proposed in) PR 59062.


 To generate a diff of this commit:
 cvs rdiff -u -r1.4 -r1.5 src/external/gpl3/gcc/dist/gcc/tree-ssa-ccp.cc

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59062 CVS commit: src/external/bsd/jemalloc/include/jemalloc/internal
Date: Thu, 6 Mar 2025 13:41:17 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Thu Mar  6 13:41:16 UTC 2025

 Modified Files:
 	src/external/bsd/jemalloc/include/jemalloc/internal: size_classes.h

 Log Message:
 PR 59062: make jemalloc guaranteed alignment match previous NetBSD
 guarantees.

 Current theory is that 1 << LG_TINY_MIN in jemalloc has to match
 gcc config's MALLOC_ABI_ALIGNMENT/8 to guarantee full alignment
 even for small allocations per the C standard's requirements
 linked from the PR.


 To generate a diff of this commit:
 cvs rdiff -u -r1.4 -r1.5 \
     src/external/bsd/jemalloc/include/jemalloc/internal/size_classes.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: martin@NetBSD.org
State-Changed-When: Sat, 04 Oct 2025 16:15:00 +0000
State-Changed-Why:
Close this, local state is fine and jemalloc upstream was unresponsive


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.49 2026/05/14 01:52:41 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.