From patchwork Tue Jan 9 21:57:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 186595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp458488dyi; Tue, 9 Jan 2024 15:04:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXM+1HkVKgKeEPEgcJXOOYbmuU/I0xekMFUifESlV4bpDXL7cxFuKsxpmHBqwa79kordsF X-Received: by 2002:a05:622a:5ce:b0:429:794d:4a76 with SMTP id d14-20020a05622a05ce00b00429794d4a76mr145486qtb.111.1704841463770; Tue, 09 Jan 2024 15:04:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704841463; cv=pass; d=google.com; s=arc-20160816; b=aFAkdWdjkB/9Z2yGIOqui/eHifwRtcbRX2LEJijDkTBfxyfsmeslQcVmDGuc4LwOpy 2XO9POnxayn8D3UW6VtxBuCT/+NqFBVZSCWwY5Qg+8J6vPlLYTlSOOVH1Pg4CXhZkVPE ybQEH3AOgKHuTKvcDPAzQPdNTW9FIh8ibX65x7TyRsHnt5iVmff68+RjUwNNqu3pzJs+ +vxQnP8o9xpG9im/lYHxFDnYUsWvY2knST5jtfhtdAd/Dj0fSEoGXDKo5HtFlq5Y+RE+ dA4VX1EC5DdLqI3i48oTS/4RuyNfZHSBWiYRoDRqTZBjmyGJRg2dWYNQKnje0OoRkE15 9CUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=J+tJ6aVColgudDOVP2HM1WCEzb19E7hU7G1cO5cEric=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=fmrpX7t4BHc7geAE41pf/99AS0D9PeFEoJnNGTKe1VXIshwcWsF7zBHc8MPN8VehJ+ 1kMeNNj3ifmqeQabaV2TwjvnmJo+gwVH6imU4d2CrY8nG/Sc7VXw1u1U91jb1JY6SHFa dxxMmHIUjGoDgLR1kPHKCJOc+1AhsGnzzpjpSHLDFgnfUrIQWLTJ6cC/nYIAPX9v1MwX u4Clo+1lLLWZpTZjFP7hhkudOFhH8qWAChWcZtioFa+eaFoH8dB6N8CAZ4H49bvpntIy To1dakmvFxd8TwXoZSGpfbJjjaSnod3mS3A0aG/fk+Y0976WrvN8M4aDGt++iHYf8y8i Cqcg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OVwE1Ovk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id i22-20020ac85c16000000b00429a6f45c2esi2156728qti.598.2024.01.09.15.04.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:04:23 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OVwE1Ovk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B81E385E021 for ; Tue, 9 Jan 2024 22:00:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 10FD3385841C for ; Tue, 9 Jan 2024 21:59:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 10FD3385841C 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 10FD3385841C 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=1704837579; cv=none; b=NBTu/C5XiGOBMj25vWb2YVc7hl1VgKzTYpeDngTp7A+wHZbChP5blTzYWkBByAxgV+8UlGs6pdgIgd56QRBgeYccExDxL27RKlE/a32BCxs5yi9Xz8NOJkyTewb/wtmWYm+WE6dXdiQwq+m7b++L35lgHHUAFnBMD3VHF3EMjA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704837579; c=relaxed/simple; bh=7HBjqHtEVKRkPNiI29P9EnA9ni5yco6aZrSBT7sFDgY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ixv0yNlIdv0nPK4Rsi0yEOiMJBclThOMmYVIcmQ0x6PyLWmPzribBZvts69c1nX2qyMIJj7Nuws3K8suYPYQlTdTXNAYflSrBlJ5JroeCJ9Wm2dqgFEsIJwfKT6HOnPgJt70Sf2Kvyew2MFefOvzV251Z6xQk5X4haH5jqlYs1c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704837577; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=J+tJ6aVColgudDOVP2HM1WCEzb19E7hU7G1cO5cEric=; b=OVwE1Ovkng/cUAjZCMSyQnI9HMyRl4XOYHaOY/dw6fQIBW49+xb54Qz5si2e9vNOXOvjfv QG6cF8E3EusrOJAplMse3W1Q6zVackqJei3KhJTjMrOG+KDE0yt88KoTx2MYOTlBMvS7DK RDhWzCUHBzSqRb0YzrUUNsQJ51mOnho= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-372-IhyS6HkvNAaO52pelBGaYQ-1; Tue, 09 Jan 2024 16:59:34 -0500 X-MC-Unique: IhyS6HkvNAaO52pelBGaYQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 30181833AE2; Tue, 9 Jan 2024 21:59:34 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECDE01C060AF; Tue, 9 Jan 2024 21:59:33 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Prefer posix_memalign for aligned-new [PR113258] Date: Tue, 9 Jan 2024 21:57:25 +0000 Message-ID: <20240109215933.4054953-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787655842412923182 X-GMAIL-MSGID: 1787655842412923182 Does anybody see any problem with making this change, so that we avoid the problem described in the PR? -- >8 -- As described in PR libstdc++/113258 there are old versions of tcmalloc which replace malloc and related APIs, but do not repalce aligned_alloc because it didn't exist at the time they were released. This means that when operator new(size_t, align_val_t) uses aligned_alloc to obtain memory, it comes from libc's aligned_alloc not from tcmalloc. But when operator delete(void*, size_t, align_val_t) uses free to deallocate the memory, that goes to tcmalloc's replacement version of free, which doesn't know how to free it. If we give preference to the older posix_memalign instead of aligned_alloc then we're more likely to use a function that will be compatible with the replacement version of free. Because posix_memalign has been around for longer, it's more likely that old third-party malloc replacements will also replace posix_memalign alongside malloc and free. libstdc++-v3/ChangeLog: PR libstdc++/113258 * libsupc++/new_opa.cc: Prefer to use posix_memalign if available. --- libstdc++-v3/libsupc++/new_opa.cc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/libsupc++/new_opa.cc b/libstdc++-v3/libsupc++/new_opa.cc index 8326b7497fe..35606e1c1b3 100644 --- a/libstdc++-v3/libsupc++/new_opa.cc +++ b/libstdc++-v3/libsupc++/new_opa.cc @@ -46,12 +46,12 @@ using std::bad_alloc; using std::size_t; extern "C" { -# if _GLIBCXX_HAVE_ALIGNED_ALLOC +# if _GLIBCXX_HAVE_POSIX_MEMALIGN + void *posix_memalign(void **, size_t alignment, size_t size); +# elif _GLIBCXX_HAVE_ALIGNED_ALLOC void *aligned_alloc(size_t alignment, size_t size); # elif _GLIBCXX_HAVE__ALIGNED_MALLOC void *_aligned_malloc(size_t size, size_t alignment); -# elif _GLIBCXX_HAVE_POSIX_MEMALIGN - void *posix_memalign(void **, size_t alignment, size_t size); # elif _GLIBCXX_HAVE_MEMALIGN void *memalign(size_t alignment, size_t size); # else @@ -63,13 +63,10 @@ extern "C" #endif namespace __gnu_cxx { -#if _GLIBCXX_HAVE_ALIGNED_ALLOC -using ::aligned_alloc; -#elif _GLIBCXX_HAVE__ALIGNED_MALLOC -static inline void* -aligned_alloc (std::size_t al, std::size_t sz) -{ return _aligned_malloc(sz, al); } -#elif _GLIBCXX_HAVE_POSIX_MEMALIGN +// Prefer posix_memalign if available, because it's older than aligned_alloc +// and so more likely to be provided by replacement malloc libraries that +// predate the addition of aligned_alloc. See PR libstdc++/113258. +#if _GLIBCXX_HAVE_POSIX_MEMALIGN static inline void* aligned_alloc (std::size_t al, std::size_t sz) { @@ -83,6 +80,12 @@ aligned_alloc (std::size_t al, std::size_t sz) return ptr; return nullptr; } +#elif _GLIBCXX_HAVE_ALIGNED_ALLOC +using ::aligned_alloc; +#elif _GLIBCXX_HAVE__ALIGNED_MALLOC +static inline void* +aligned_alloc (std::size_t al, std::size_t sz) +{ return _aligned_malloc(sz, al); } #elif _GLIBCXX_HAVE_MEMALIGN static inline void* aligned_alloc (std::size_t al, std::size_t sz) @@ -128,7 +131,8 @@ operator new (std::size_t sz, std::align_val_t al) if (__builtin_expect (sz == 0, false)) sz = 1; -#if _GLIBCXX_HAVE_ALIGNED_ALLOC +#if _GLIBCXX_HAVE_POSIX_MEMALIGN +#elif _GLIBCXX_HAVE_ALIGNED_ALLOC # if defined _AIX || defined __APPLE__ /* AIX 7.2.0.0 aligned_alloc incorrectly has posix_memalign's requirement * that alignment is a multiple of sizeof(void*).