From patchwork Tue May 2 19:01:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1776010 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=E22LrC5S; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q9qFW0gqnz1ydX for ; Wed, 3 May 2023 05:02:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 716B63858C62 for ; Tue, 2 May 2023 19:02:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 716B63858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683054121; bh=vNM6aED9YnI8+riC+ZpjE73Q8xFLfFpztTMTwO7H6h4=; h=To:Cc:Subject:In-Reply-To:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=E22LrC5SS0jzGBlj2uP/ntfZcgPqOLJf/NEjkvtPestOxJOl7mzw3ma1T5KiokxQf XNJuShd0NQ6oqHG4R2M0GHNxl1HYUzwBP2GwXAGDTDiP3cZIm03n4ge5bN/jGK1MM9 rkOWGResOP0DPCnU0/RU71aLSq+qwlmaWUfQ730Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4CD9B3858D1E for ; Tue, 2 May 2023 19:01:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CD9B3858D1E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-93-gLszTxo2MUOYsq5bUM6EVA-1; Tue, 02 May 2023 15:01:43 -0400 X-MC-Unique: gLszTxo2MUOYsq5bUM6EVA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 61DE1185A7A2 for ; Tue, 2 May 2023 19:01:43 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.103]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4278B492C13; Tue, 2 May 2023 19:01:43 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 342J1gJ71382235; Tue, 2 May 2023 15:01:42 -0400 To: "Carlos O'Donell" Cc: libc-alpha@sourceware.org Subject: [patch v5] aligned_alloc: conform to C17 In-Reply-To: <53234b02-ffa6-13e9-0b9c-87325e15dc91@redhat.com> Date: Tue, 02 May 2023 15:01:42 -0400 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: DJ Delorie via Libc-alpha From: DJ Delorie Reply-To: DJ Delorie Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Changes as requested... From a2c1315544c72dedfcbd94b2efa7afb3eb1f0a02 Mon Sep 17 00:00:00 2001 From: DJ Delorie Date: Tue, 21 Mar 2023 00:46:43 -0400 Subject: aligned_alloc: conform to C17 This patch adds the strict checking for power-of-two alignments in aligned_alloc(), and updates the manual accordingly. Reviewed-by: Carlos O'Donell diff --git a/malloc/Makefile b/malloc/Makefile index f4cae2538c..57a9677512 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -44,11 +44,13 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ tst-safe-linking \ tst-mallocalign1 \ tst-memalign-2 \ - tst-memalign-3 + tst-memalign-3 \ + tst-aligned-alloc tests-static := \ tst-interpose-static-nothread \ - tst-interpose-static-thread + tst-interpose-static-thread \ + tst-aligned-alloc-static # Test for the malloc_set_state symbol removed in glibc 2.25. ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes) diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c index 3867d15698..da9d2340d3 100644 --- a/malloc/malloc-debug.c +++ b/malloc/malloc-debug.c @@ -299,7 +299,14 @@ __debug_memalign (size_t alignment, size_t bytes) return _debug_mid_memalign (alignment, bytes, RETURN_ADDRESS (0)); } strong_alias (__debug_memalign, memalign) -strong_alias (__debug_memalign, aligned_alloc) +static void * +__debug_aligned_alloc (size_t alignment, size_t bytes) +{ + if (!powerof2 (alignment) || alignment == 0) + return NULL; + return _debug_mid_memalign (alignment, bytes, RETURN_ADDRESS (0)); +} +strong_alias (__debug_aligned_alloc, aligned_alloc) static void * __debug_pvalloc (size_t bytes) diff --git a/malloc/malloc.c b/malloc/malloc.c index e33ed665db..5d8b61d66c 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3528,6 +3528,29 @@ __libc_memalign (size_t alignment, size_t bytes) void *address = RETURN_ADDRESS (0); return _mid_memalign (alignment, bytes, address); } +libc_hidden_def (__libc_memalign) + +/* For ISO C17. */ +void * +weak_function +aligned_alloc (size_t alignment, size_t bytes) +{ + if (!__malloc_initialized) + ptmalloc_init (); + +/* Similar to memalign, but starting with ISO C17 the standard + requires an error for alignments that are not supported by the + implementation. Valid alignments for the current implementation + are non-negative powers of two. */ + if (!powerof2 (alignment) || alignment == 0) + { + __set_errno (EINVAL); + return 0; + } + + void *address = RETURN_ADDRESS (0); + return _mid_memalign (alignment, bytes, address); +} static void * _mid_memalign (size_t alignment, size_t bytes, void *address) @@ -3618,9 +3641,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) ar_ptr == arena_for_chunk (mem2chunk (p))); return tag_new_usable (p); } -/* For ISO C11. */ -weak_alias (__libc_memalign, aligned_alloc) -libc_hidden_def (__libc_memalign) void * __libc_valloc (size_t bytes) diff --git a/malloc/tst-aligned-alloc-static.c b/malloc/tst-aligned-alloc-static.c new file mode 100644 index 0000000000..d504473094 --- /dev/null +++ b/malloc/tst-aligned-alloc-static.c @@ -0,0 +1 @@ +#include "tst-aligned-alloc.c" diff --git a/malloc/tst-aligned-alloc.c b/malloc/tst-aligned-alloc.c new file mode 100644 index 0000000000..8bd6527147 --- /dev/null +++ b/malloc/tst-aligned-alloc.c @@ -0,0 +1,80 @@ +/* Test for C17 alignment requirements. + Copyright (C) 2023 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + void *p1; + void *p2; + void *p3; + void *p4; + void *p5; + + errno = 0; + + /* The implementation supports alignments that are non-negative powers of 2. + We test 5 distinct conditions here: + - A non-negative power of 2 alignment e.g. 64. + - A degenerate zero power of 2 alignment e.g. 1. + - A non-power-of-2 alignment e.g. 65. + - A zero alignment. + - A corner case SIZE_MAX / 2 + 1 alignment. + */ + + p1 = aligned_alloc (64, 64); + + if (p1 == NULL) + FAIL_EXIT1 ("aligned_alloc(64, 64) failed"); + + p2 = aligned_alloc (1, 64); + + if (p2 == NULL) + FAIL_EXIT1 ("aligned_alloc(1, 64) failed"); + + p3 = aligned_alloc (65, 64); + + if (p3 != NULL) + FAIL_EXIT1 ("aligned_alloc(65, 64) did not fail"); + + p4 = aligned_alloc (0, 64); + + if (p4 != NULL) + FAIL_EXIT1 ("aligned_alloc(0, 64) did not fail"); + + /* This is an alignment like 0x80000000...UL */ + p5 = aligned_alloc (SIZE_MAX / 2 + 1, 64); + + if (p5 != NULL) + FAIL_EXIT1 ("aligned_alloc(SIZE_MAX/2+1, 64) did not fail"); + + free (p1); + free (p2); + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/manual/memory.texi b/manual/memory.texi index 9d3398a326..8952ff2bfa 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -995,7 +995,7 @@ power of two than that, use @code{aligned_alloc} or @code{posix_memalign}. @c Alias to memalign. The @code{aligned_alloc} function allocates a block of @var{size} bytes whose address is a multiple of @var{alignment}. The @var{alignment} must be a -power of two and @var{size} must be a multiple of @var{alignment}. +power of two. The @code{aligned_alloc} function returns a null pointer on error and sets @code{errno} to one of the following values: