From patchwork Mon Sep 27 14:10:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 45472 X-Patchwork-Delegate: jwakely.gcc@gmail.com 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 A60D33858409 for ; Mon, 27 Sep 2021 14:10:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from ext-mx-out002.mykolab.com (mx.kolabnow.com [95.128.36.41]) by sourceware.org (Postfix) with ESMTPS id 9EC273858D3C; Mon, 27 Sep 2021 14:10:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9EC273858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=appliantology.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=appliantology.com Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 456F6B34; Mon, 27 Sep 2021 16:10:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1632751838; x=1634566239; bh=6sJi/UeF3qHfBbOUHInHbae+2OIhmSVMsnzhIyA8hcw=; b=4rHOuVnoEcwm cmQ9uy1KisXfgO4GIg80y8YYBpisJjOI8s2sodyFlCYoQ09W3V/28j3bkOBJf9te 40iex7z7YW7l03m2wHYNEGEPrnS1tQ+4GM3yP7pN+bHSmwIAU2Zy72KH81pXeAIU 45GmsrMqaW05IwuUadbnXrIjmGvwRVkBVuHzvBAok3QYaz1mruk94Z5Vtq18mK8L 5sEzyBVcitOK/MKnfRLUOxb9W1mHQfarVc0ZPJj7m2vSG43AMTuY0ygvbVyOr8Jr 3b+bFbs0Mtsl1Q2HLVYCBcpAqkvrq0Ig77cXptDn9Kl4s2+OFOTn6f2ph+s0vycY ZnmsiNiKrcG1ZgbT6NL36TsSMljFOaQ7dTDpjeOpA3SSCNC6cVcUucHUP0EZre0Q 7/aqs8SHSmsQRHaEiYOcES3eLbaYEzgd/L+aQjmWwtLhtG162Ek/z8GklwIj4Qt3 6JuP7TUZA2gmBlY2UNODnhYvc96N7Sph0uxIUt2phHTVbd1JBri5D0yhz9skxx8t or6eZZvCSBu7Mrv06bu8e8uudCqtMq6Im6zP1bWj0dhEvTJMLkKxPhl4FwGWT5kf 01anGIb+IymYcSqf8nIfjt/dV+AUbsR4N+xFdNi5uvXH87wWn4blv2CyH3M9jakq oJ86xGFyNd755qFR9xN0IPrvC31m73c= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -1.9 X-Spam-Level: X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 Received: from ext-mx-out002.mykolab.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id X9hR5xRs0b-p; Mon, 27 Sep 2021 16:10:38 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 834196D1; Mon, 27 Sep 2021 16:10:38 +0200 (CEST) Received: from ext-subm001.mykolab.com (unknown [10.9.6.1]) by int-mx003.mykolab.com (Postfix) with ESMTPS id 4A57B253B; Mon, 27 Sep 2021 16:10:37 +0200 (CEST) From: Thomas Rodgers To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Clear padding bits in atomic compare_exchange Date: Mon, 27 Sep 2021 07:10:31 -0700 Message-Id: <20210927141031.651313-1-rodgert@appliantology.com> In-Reply-To: <20210923180837.633173-1-rodgert@appliantology.com> References: <20210923180837.633173-1-rodgert@appliantology.com> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: trodgers@redhat.com, Thomas Rodgers Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Thomas Rodgers Now with checks for __has_builtin(__builtin_clear_padding) This change implements P0528 which requires that padding bits not participate in atomic compare exchange operations. All arguments to the generic template are 'sanitized' by the __builtin_clearpadding intrisic before they are used in comparisons. This alrequires that any stores also sanitize the incoming value. Signed-off-by: Thomas Rodgers libstdc++=v3/ChangeLog: * include/std/atomic (atomic::atomic(_Tp) clear padding for __cplusplus > 201703L. (atomic::store()) Clear padding. (atomic::exchange()) Likewise. (atomic::compare_exchange_weak()) Likewise. (atomic::compare_exchange_strong()) Likewise. * testsuite/29_atomics/atomic/compare_exchange_padding.cc: New test. --- libstdc++-v3/include/std/atomic | 41 +++++++++++++++++- .../atomic/compare_exchange_padding.cc | 42 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic index 936dd50ba1c..4ac9ccdc1ab 100644 --- a/libstdc++-v3/include/std/atomic +++ b/libstdc++-v3/include/std/atomic @@ -228,7 +228,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION atomic& operator=(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - constexpr atomic(_Tp __i) noexcept : _M_i(__i) { } +#if __cplusplus > 201703L && __has_builtin(__builtin_clear_padding) + constexpr atomic(_Tp __i) noexcept : _M_i(__i) + { __builtin_clear_padding(std::__addressof(_M_i)); } +#else + constexpr atomic(_Tp __i) noexcept : _M_i(__i) + { } +#endif operator _Tp() const noexcept { return load(); } @@ -268,12 +274,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void store(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept { +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__i)); +#endif __atomic_store(std::__addressof(_M_i), std::__addressof(__i), int(__m)); } void store(_Tp __i, memory_order __m = memory_order_seq_cst) volatile noexcept { +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__i)); +#endif __atomic_store(std::__addressof(_M_i), std::__addressof(__i), int(__m)); } @@ -300,6 +312,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { alignas(_Tp) unsigned char __buf[sizeof(_Tp)]; _Tp* __ptr = reinterpret_cast<_Tp*>(__buf); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__i)); +#endif __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i), __ptr, int(__m)); return *__ptr; @@ -311,6 +326,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { alignas(_Tp) unsigned char __buf[sizeof(_Tp)]; _Tp* __ptr = reinterpret_cast<_Tp*>(__buf); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__i)); +#endif __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i), __ptr, int(__m)); return *__ptr; @@ -322,6 +340,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __glibcxx_assert(__is_valid_cmpexch_failure_order(__f)); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__e)); + __builtin_clear_padding(std::__addressof(__i)); +#endif return __atomic_compare_exchange(std::__addressof(_M_i), std::__addressof(__e), std::__addressof(__i), @@ -334,6 +356,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __glibcxx_assert(__is_valid_cmpexch_failure_order(__f)); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__e)); + __builtin_clear_padding(std::__addressof(__i)); +#endif return __atomic_compare_exchange(std::__addressof(_M_i), std::__addressof(__e), std::__addressof(__i), @@ -358,6 +384,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __glibcxx_assert(__is_valid_cmpexch_failure_order(__f)); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__e)); + __builtin_clear_padding(std::__addressof(__i)); +#endif return __atomic_compare_exchange(std::__addressof(_M_i), std::__addressof(__e), std::__addressof(__i), @@ -370,6 +400,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __glibcxx_assert(__is_valid_cmpexch_failure_order(__f)); +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__e)); + __builtin_clear_padding(std::__addressof(__i)); +#endif return __atomic_compare_exchange(std::__addressof(_M_i), std::__addressof(__e), std::__addressof(__i), @@ -392,6 +426,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept { +#if __has_builtin(__builtin_clear_padding) + __builtin_clear_padding(std::__addressof(__old)); +#endif std::__atomic_wait_address_v(&_M_i, __old, [__m, this] { return this->load(__m); }); } @@ -407,7 +444,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::__atomic_notify_address(&_M_i, true); } #endif // __cpp_lib_atomic_wait - }; + }; #undef _GLIBCXX20_INIT /// Partial specialization for pointer types. diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc b/libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc new file mode 100644 index 00000000000..0875f168097 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc @@ -0,0 +1,42 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } +// { dg-add-options libatomic } + +#include + +#include + +struct S { char c; short s; }; + +void __attribute__((noinline,noipa)) +fill_struct(S& s) +{ __builtin_memset(&s, 0xff, sizeof(S)); } + +bool +compare_struct(const S& a, const S& b) +{ return __builtin_memcmp(&a, &b, sizeof(S)) == 0; } + +int +main () +{ + S s; + fill_struct(s); + s.c = 'a'; + s.s = 42; + + std::atomic as{ s }; + auto ts = as.load(); + VERIFY( !compare_struct(s, ts) ); // padding cleared on construction + as.exchange(s); + auto es = as.load(); + VERIFY( compare_struct(ts, es) ); // padding cleared on exchange + + S n; + fill_struct(n); + n.c = 'b'; + n.s = 71; + // padding cleared on compexchg + VERIFY( as.compare_exchange_weak(s, n) ); + VERIFY( as.compare_exchange_strong(n, s) ); + return 0; +}