libstdc++: Clear padding bits in atomic compare_exchange
authorThomas Rodgers <trodgers@redhat.com>
Thu, 25 Aug 2022 10:11:40 +0000 (12:11 +0200)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 8 Sep 2022 18:29:12 +0000 (19:29 +0100)
commit157236dbd621644b3cec50b6cf38811959f3e78c
treeaf9e5ef2a4382cb1af7d6083b3945936e60d18f5
parentd3883dc77b1426984c0edea6081f57ed2305c9f2
libstdc++: Clear padding bits in atomic compare_exchange

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_clear_padding intrinsic
before they are used in comparisons. This requires that any stores
also sanitize the incoming value.

Co-authored-by: Jakub Jelinek <jakub@redhat.com>
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Thomas Rodgers <trodgers@redhat.com>
libstdc++-v3/ChangeLog:

* include/bits/atomic_base.h (__atomic_impl::__maybe_has_padding):
New function.
(__atomic_impl::clear_padding): Likewise.
(__atomic_impl::__compare_exchange): Likewise.
(__atomic_impl::compare_exchange_weak): Delegate to
__compare_exchange.
(__atomic_impl::compare_exchange_strong): Likewise.
* include/std/atomic (atomic<T>::atomic(T)): Clear padding when
possible in a constexpr function.
(atomic::store): Clear padding.
(atomic::exchange): Likewise.
(atomic::compare_exchange_weak): Use __compare_exchange.
(atomic::compare_exchange_strong): Likewise.
* testsuite/29_atomics/atomic/compare_exchange_padding.cc: New
test.
* testsuite/29_atomics/atomic_ref/compare_exchange_padding.cc:
New test.
libstdc++-v3/include/bits/atomic_base.h
libstdc++-v3/include/std/atomic
libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc [new file with mode: 0644]
libstdc++-v3/testsuite/29_atomics/atomic_ref/compare_exchange_padding.cc [new file with mode: 0644]