From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0440A3858D33 for ; Tue, 21 May 2024 08:38:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0440A3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0440A3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716280699; cv=none; b=tBO1GxVkuKkVg3BIsP+F89+siP+fKe/ar+wdWg3FHocehY9icOlB8kstMVdAZ3IYEfyLBvSxHmvHvRIL/enbCzfHcAV/vv6+rbwnIFKAr2AYGxE3kxUAQX0nV/Y3AKEMlbWhOkXRwXHnOzBh04T9asdYfeOp0POjnXUoJPjUT8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716280699; c=relaxed/simple; bh=3zM/+YWoWyPsrHy3ACj48dTirhK1fWFfSEanP7XqTF8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=IyXqDcU4ShVXh3p1/iC84/p55Isf0iWvHcOaah4q+AQVwf4H6MwSp7Tcpm19/m7jK1FzLP33vLt9OO44wEu2JApYm+MEZ18SklYEh+kx3xIkt88CW4Z55z/MuKlCzdT92rbf2IYwQQrLEI7u2zRBDEh/s6RokD+DP3psvKKIWPs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716280696; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f50FHaFvn6sBs7cmajzMPQ6IDlcgd8inxoU1R8uOiuY=; b=atYixfjf6+ZdHranz7arRWtYXh7nplZkXJG5DdcmIfrg4CbmWw9upurgkEDWCkAlGf2Mda aAGZ+0Odfrv8A4XF0wcBgPf4h2wI7K2TmIbbGLGql1AsBnUke4gjDylsnze6xNzjadPHtO y7BR2p9G3nW8sPAEN6NQCyGNUFNak4k= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-218-6TxHY4o-OkOWMMjrAoQK7w-1; Tue, 21 May 2024 04:38:15 -0400 X-MC-Unique: 6TxHY4o-OkOWMMjrAoQK7w-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-351cb5b7649so5261165f8f.3 for ; Tue, 21 May 2024 01:38:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716280693; x=1716885493; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f50FHaFvn6sBs7cmajzMPQ6IDlcgd8inxoU1R8uOiuY=; b=C5UdT7SKVfHT1KJid8SQGDLElYBiETSLh0jpS84Jvn0FPNyQ3jV272dq13NHXK7oEI 2nci1hbEpGVDGnZU5JuDFKTJyEy72ti7LTAyjrpSUBZZe62drG/Q4QtfJeiu6HNQDS5O 3/260XrL4BodPDek54zaEyFL4/MZijdrH7DbRtlxNkSy9YZW4ptJ3CdB26UHwctJFdtY 3hf8pNpAsyAJNVZ64z33by4fmjazx12sAhlxVECW2T/h68vxfjr59WtqZ7FrjY9VfuSP z2RUiybKHct5GXujju8r1w9CEPLBch8CSm9cXaY/6qCtdg3UpiK8hNkFSajw4jqXReo0 btPA== X-Gm-Message-State: AOJu0YzWzSXEmk6A6mkpAI5k1k4niR3rMmsyMPWyAgz6N2btSVMGSU/z nOW9tvBncwKHOC2iUeUVK0xjPPDvbzcqAPzg546LsheKI85DkzJsFSU6/V1BbDiEyKemH1nmLDg yWa3SCJWc8ZbLaQxsjyj9oHmor7xoulXn4q76f1hf/dbBle4DCmUlcRIa0WRpOTgMFBoIJb0d7I 9fdEe0zEFLNCHHgLMpQRdWcU/UXmTEZAYtlEyapd/pebA= X-Received: by 2002:a5d:52d1:0:b0:34e:4105:b3f6 with SMTP id ffacd0b85a97d-3504a73ee8dmr20143140f8f.39.1716280692337; Tue, 21 May 2024 01:38:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgVeGu2tfpacqzRlLrWhV9OlA5E8Z2dVLlBoTeVYAzN8/iKj2q6ECSKRXUSNqxENt5bVJdyw== X-Received: by 2002:a5d:52d1:0:b0:34e:4105:b3f6 with SMTP id ffacd0b85a97d-3504a73ee8dmr20143124f8f.39.1716280691819; Tue, 21 May 2024 01:38:11 -0700 (PDT) Received: from lenovo-p1.mamux.org ([82.213.234.88]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502b8a7748sm31491657f8f.49.2024.05.21.01.38.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 01:38:11 -0700 (PDT) From: Miguel Martin To: libc-stable@sourceware.org Cc: DJ Delorie , Carlos O'Donell Subject: [COMMITTED 2.34 1/2] aligned_alloc: conform to C17 Date: Tue, 21 May 2024 10:38:06 +0200 Message-ID: <20240521083807.925020-2-mmartinv@redhat.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240521083807.925020-1-mmartinv@redhat.com> References: <20240521083807.925020-1-mmartinv@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: DJ Delorie This patch adds the strict checking for power-of-two alignments in aligned_alloc(), and updates the manual accordingly. Reviewed-by: Carlos O'Donell (cherry picked from commit d1417176a35d27ffb8da0ffb1e33154163b6eeb2) --- malloc/Makefile | 4 +- malloc/malloc-debug.c | 9 +++- malloc/malloc.c | 26 ++++++++-- malloc/tst-aligned-alloc-static.c | 1 + malloc/tst-aligned-alloc.c | 80 +++++++++++++++++++++++++++++++ manual/memory.texi | 2 +- 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 malloc/tst-aligned-alloc-static.c create mode 100644 malloc/tst-aligned-alloc.c diff --git a/malloc/Makefile b/malloc/Makefile index 9b70831d38..b02c49a687 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -43,10 +43,12 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ tst-tcfree1 tst-tcfree2 tst-tcfree3 \ tst-safe-linking \ tst-mallocalign1 \ + 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 3d7e6d44fd..95ad42b68e 100644 --- a/malloc/malloc-debug.c +++ b/malloc/malloc-debug.c @@ -300,7 +300,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 d31e985ecc..5ce3223777 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3407,6 +3407,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) @@ -3465,9 +3488,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 0b2b9c8795..5c16f7aee6 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: -- 2.45.1