From patchwork Fri Jul 9 13:31:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 44297 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 71DF7384800F for ; Fri, 9 Jul 2021 13:32:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 71DF7384800F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625837572; bh=c8hmKyqiVA2oRjy84NV8fnp4onLfisyFAD6y93WcYiU=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ne9BrjQQoqaZf8qCoYCsOLTsl3ldara0xMVm8D/wVcxQypzzYG9yrqbrBZA9G2z9W O6WfentQ+Z60pTXZFeKtDVpO9z06gR4OgnunxLOQE/bUrWt6MrAiKVY4nQXfdnW0g5 Ra3jTnnEN4q+MZwTw1NRXKxJ0fZnlbnd7lSCjElE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 1551D384A02B for ; Fri, 9 Jul 2021 13:32:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1551D384A02B Received: by mail-pg1-x532.google.com with SMTP id v7so9987626pgl.2 for ; Fri, 09 Jul 2021 06:32:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=c8hmKyqiVA2oRjy84NV8fnp4onLfisyFAD6y93WcYiU=; b=Q6EtCYTxDZYd2EDyHmLoOQINaJkci26kvf9KEBJBkvHVkfTAJgHysRkDbHLtgHHhGf 21FpiRrbnQiuzu7J+7gCuzRFjh5wt9OUv46PpSMl9oNjTETwP2X1w5G5QdE7E08e+qP9 6s5j+9Q1t3J3bNGg8i4A+NsDC8fg4v08O1oMcu7KAyRjCYPVtqqKTvo3GcIKvx+fkotP Fgq2StBLiNODysJGpozoGsKGsvjZFyIlULimt6kO6Qg/v22SzfL7qZl3MvZRhwf56PXK H9b1TMOwRd9BNs/DV8wve+N0+9PAg7CnEW6v1KcjTZv9jQydO1ePk7c1bti/xvdVdR/r rSEw== X-Gm-Message-State: AOAM530u4zrYVKaXCJeiZoEGWo0l9N9E7pkcJd+Glui3f1RChcUY75C6 t/ZUxI9CDYuMVWWcRs0kQPe/dKBYGPaLXsi2p1wOWFz+QmY= X-Google-Smtp-Source: ABdhPJx/yOM0JzZjL7Ze/BnyigCfLBNCr3aotv/aKZDONtwGri+R733mU5sU51Uglg6sbngqakDmgLXalXNUZVym9Sg= X-Received: by 2002:a05:6a00:1c5f:b029:327:bd67:9d6f with SMTP id s31-20020a056a001c5fb0290327bd679d6fmr9558297pfw.48.1625837549134; Fri, 09 Jul 2021 06:32:29 -0700 (PDT) MIME-Version: 1.0 References: <20210702023546.3081774-1-siddhesh@sourceware.org> <20210702023546.3081774-3-siddhesh@sourceware.org> <0270d99b-3a15-09d2-7fa9-c9ecf2591d8f@gotplt.org> <433e43a7-e06a-cd08-ee80-7cb84d9ed7e8@gotplt.org> <6c506984-acbf-706d-1266-5182de7811e1@gotplt.org> <24dbe46e-73f2-d35a-065c-e530d5a7d409@gotplt.org> In-Reply-To: <24dbe46e-73f2-d35a-065c-e530d5a7d409@gotplt.org> Date: Fri, 9 Jul 2021 06:31:53 -0700 Message-ID: Subject: [PATCH v3] Add a generic malloc test for MALLOC_ALIGNMENT To: Siddhesh Poyarekar X-Spam-Status: No, score=-3032.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Stefan Liebler , GNU C Library Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" On Fri, Jul 9, 2021 at 6:00 AM Siddhesh Poyarekar wrote: > > On 7/9/21 5:54 PM, H.J. Lu wrote: > > On Thu, Jul 8, 2021 at 9:07 PM Siddhesh Poyarekar wrote: > >> > >>> diff --git a/malloc/Makefile b/malloc/Makefile > >>> index 37a9a4efab..b685ed6d61 100644 > >>> --- a/malloc/Makefile > >>> +++ b/malloc/Makefile > >>> @@ -42,6 +42,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ > >>> tst-malloc-stats-cancellation \ > >>> tst-tcfree1 tst-tcfree2 tst-tcfree3 \ > >>> tst-safe-linking \ > >>> + tst-mallocalign1 \ > >>> > >> > >> Please remove the trailing '\'. OK otherwise. > >> > > > > Changes in the v2 patch > > > > 1. Add a comment in malloc/tst-mallocalign1.c to indicate that it is > > used to verify that MALLOC_ALIGNMENT is honored by malloc. > > 2. Include in malloc-size.h after SIZE_SZ is > > defined since may use SIZE_SZ. > > > > BTW, I kept the trailing '\' since it is used in all other places in > > malloc/Makefile. > > > > OK for master? > > I forgot to ask, does it run successfully on i686 and arm? IIRC it too > requires 16 byte alignment and hence both i686 and x32 will fail. It fails on i686 and x32. > If they don't succeed then it may make sense to add > > tests-exclude-mcheck += tst-mallocalign1 > > for x32 and i686. > > Siddhesh Here is the v3 patch to add tst-mallocalign1 to tests-exclude-mcheck for i386 and x32. OK for master? From 8343b90bf0180c011838259fa29aa5958337e656 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Thu, 8 Jul 2021 20:48:14 -0700 Subject: [PATCH v3] Add a generic malloc test for MALLOC_ALIGNMENT 1. Add sysdeps/generic/malloc-size.h to define size related macros for malloc. 2. Move x86_64/tst-mallocalign1.c to malloc and replace ALIGN_MASK with MALLOC_ALIGN_MASK. 3. Add tst-mallocalign1 to tests-exclude-mcheck for i386 and x32 since mcheck doesn't honor MALLOC_ALIGNMENT. --- malloc/Makefile | 1 + malloc/malloc-internal.h | 41 +----------- {sysdeps/x86_64 => malloc}/tst-mallocalign1.c | 31 +++++---- sysdeps/generic/malloc-machine.h | 1 - sysdeps/generic/malloc-size.h | 64 +++++++++++++++++++ sysdeps/i386/Makefile | 4 ++ sysdeps/x86_64/Makefile | 4 -- sysdeps/x86_64/x32/Makefile | 4 ++ 8 files changed, 89 insertions(+), 61 deletions(-) rename {sysdeps/x86_64 => malloc}/tst-mallocalign1.c (65%) create mode 100644 sysdeps/generic/malloc-size.h diff --git a/malloc/Makefile b/malloc/Makefile index 37a9a4efab..b685ed6d61 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -42,6 +42,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ tst-malloc-stats-cancellation \ tst-tcfree1 tst-tcfree2 tst-tcfree3 \ tst-safe-linking \ + tst-mallocalign1 \ tests-static := \ tst-interpose-static-nothread \ diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h index 258f29584e..0c7b5a183c 100644 --- a/malloc/malloc-internal.h +++ b/malloc/malloc-internal.h @@ -21,46 +21,7 @@ #include #include - -/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of - chunk sizes. - - The default version is the same as size_t. - - While not strictly necessary, it is best to define this as an - unsigned type, even if size_t is a signed type. This may avoid some - artificial size limitations on some systems. - - On a 64-bit machine, you may be able to reduce malloc overhead by - defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the - expense of not being able to handle more than 2^32 of malloced - space. If this limitation is acceptable, you are encouraged to set - this unless you are on a platform requiring 16byte alignments. In - this case the alignment requirements turn out to negate any - potential advantages of decreasing size_t word size. - - Implementors: Beware of the possible combinations of: - - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits, - and might be the same width as int or as long - - size_t might have different width and signedness as INTERNAL_SIZE_T - - int and long might be 32 or 64 bits, and might be the same width - - To deal with this, most comparisons and difference computations - among INTERNAL_SIZE_Ts should cast them to unsigned long, being - aware of the fact that casting an unsigned int to a wider long does - not sign-extend. (This also makes checking for negative numbers - awkward.) Some of these casts result in harmless compiler warnings - on some systems. */ -#ifndef INTERNAL_SIZE_T -# define INTERNAL_SIZE_T size_t -#endif - -/* The corresponding word size. */ -#define SIZE_SZ (sizeof (INTERNAL_SIZE_T)) - -/* The corresponding bit mask value. */ -#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1) - +#include /* Called in the parent process before a fork. */ void __malloc_fork_lock_parent (void) attribute_hidden; diff --git a/sysdeps/x86_64/tst-mallocalign1.c b/malloc/tst-mallocalign1.c similarity index 65% rename from sysdeps/x86_64/tst-mallocalign1.c rename to malloc/tst-mallocalign1.c index 33a6375777..294e821afe 100644 --- a/sysdeps/x86_64/tst-mallocalign1.c +++ b/malloc/tst-mallocalign1.c @@ -1,4 +1,5 @@ -/* Copyright (C) 2012-2021 Free Software Foundation, Inc. +/* Verify that MALLOC_ALIGNMENT is honored by malloc. + Copyright (C) 2012-2021 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -17,17 +18,16 @@ #include #include +#include +#include -/* Specified by x86-64 psABI. */ -#define ALIGN_MASK (16 - 1) - -void * +static void * test (size_t s) { void *p = malloc (s); - printf ("malloc: %ld, %p: %ld\n", (unsigned long) s, p, - ((unsigned long) p) & ALIGN_MASK); + printf ("malloc: %zu, %p: %zu\n", s, p, + ((uintptr_t) p) & MALLOC_ALIGN_MASK); return p; } @@ -38,35 +38,34 @@ do_test (void) int ret = 0; p = test (2); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (8); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (13); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (16); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (23); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (43); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); p = test (123); - ret |= (unsigned long) p & ALIGN_MASK; + ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; free (p); return ret; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include diff --git a/sysdeps/generic/malloc-machine.h b/sysdeps/generic/malloc-machine.h index a71f2361f5..121d8ad0c9 100644 --- a/sysdeps/generic/malloc-machine.h +++ b/sysdeps/generic/malloc-machine.h @@ -21,7 +21,6 @@ #define _GENERIC_MALLOC_MACHINE_H #include -#include #ifndef atomic_full_barrier # define atomic_full_barrier() __asm ("" ::: "memory") diff --git a/sysdeps/generic/malloc-size.h b/sysdeps/generic/malloc-size.h new file mode 100644 index 0000000000..41901b9a44 --- /dev/null +++ b/sysdeps/generic/malloc-size.h @@ -0,0 +1,64 @@ +/* Define INTERNAL_SIZE_T, SIZE_SZ, MALLOC_ALIGNMENT and MALLOC_ALIGN_MASK + for malloc. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _GENERIC_MALLOC_SIZE_H +#define _GENERIC_MALLOC_SIZE_H + +/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of + chunk sizes. + + The default version is the same as size_t. + + While not strictly necessary, it is best to define this as an + unsigned type, even if size_t is a signed type. This may avoid some + artificial size limitations on some systems. + + On a 64-bit machine, you may be able to reduce malloc overhead by + defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the + expense of not being able to handle more than 2^32 of malloced + space. If this limitation is acceptable, you are encouraged to set + this unless you are on a platform requiring 16byte alignments. In + this case the alignment requirements turn out to negate any + potential advantages of decreasing size_t word size. + + Implementors: Beware of the possible combinations of: + - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits, + and might be the same width as int or as long + - size_t might have different width and signedness as INTERNAL_SIZE_T + - int and long might be 32 or 64 bits, and might be the same width + + To deal with this, most comparisons and difference computations + among INTERNAL_SIZE_Ts should cast them to unsigned long, being + aware of the fact that casting an unsigned int to a wider long does + not sign-extend. (This also makes checking for negative numbers + awkward.) Some of these casts result in harmless compiler warnings + on some systems. */ +#ifndef INTERNAL_SIZE_T +# define INTERNAL_SIZE_T size_t +#endif + +/* The corresponding word size. */ +#define SIZE_SZ (sizeof (INTERNAL_SIZE_T)) + +#include + +/* The corresponding bit mask value. */ +#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1) + +#endif /* _GENERIC_MALLOC_SIZE_H */ diff --git a/sysdeps/i386/Makefile b/sysdeps/i386/Makefile index a2e8c0b128..8fb65c0f17 100644 --- a/sysdeps/i386/Makefile +++ b/sysdeps/i386/Makefile @@ -5,6 +5,10 @@ asm-CPPFLAGS += -DGAS_SYNTAX # The i386 `long double' is a distinct type we support. long-double-fcts = yes +ifeq ($(subdir),malloc) +tests-exclude-mcheck += tst-mallocalign1 +endif + ifeq ($(subdir),math) # These functions change the rounding mode internally and need to # update both the SSE2 rounding mode and the 387 rounding mode. See diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile index 3fc03f4a19..22c5b63ab5 100644 --- a/sysdeps/x86_64/Makefile +++ b/sysdeps/x86_64/Makefile @@ -13,10 +13,6 @@ sysdep_routines += _mcount sysdep_noprof += _mcount endif -ifeq ($(subdir),malloc) -tests += tst-mallocalign1 -endif - ifeq ($(subdir),string) sysdep_routines += strcasecmp_l-nonascii strncase_l-nonascii gen-as-const-headers += locale-defines.sym diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile index 8748956563..62919c79f1 100644 --- a/sysdeps/x86_64/x32/Makefile +++ b/sysdeps/x86_64/x32/Makefile @@ -1,3 +1,7 @@ +ifeq ($(subdir),malloc) +tests-exclude-mcheck += tst-mallocalign1 +endif + ifeq ($(subdir),math) # Since x32 returns 32-bit long int and 64-bit long long int in the # same 64-bit register, we make the 32b-bit lround an alias of the -- 2.31.1