libstdc++: Fix aliasing violation in std::shared_ptr

Message ID 20220121134933.172441-1-jwakely@redhat.com
State Committed
Commit 2d8a9ad4a922e3248d0f6c60a6331be6f47dc435
Headers
Series libstdc++: Fix aliasing violation in std::shared_ptr |

Commit Message

Jonathan Wakely Jan. 21, 2022, 1:49 p.m. UTC
  Tested powerpc64le-linux. Does anybody see a problem with this change?


The non-atomic store that sets both reference counts to zero uses a
type-punned pointer, which has undefined behaviour. We could use memset
to write 8 bytes, but we don't actually need it to be a single store
anyway. No other thread can observe the values, that's why it's safe to
use non-atomic stores in the first place. So we can just set each count
to zero.

With -fstore-merging (which is enabled by default at -O2) GCC produces
the same code for this as for memset or the type punned store. Clang
does that store merging even at -O1.

libstdc++-v3/ChangeLog:

	* include/bits/shared_ptr_base.h (_Sp_counted_base<>::_M_release):
	Set members to zero without type punning.
---
 libstdc++-v3/include/bits/shared_ptr_base.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Jonathan Wakely Jan. 23, 2022, 10:51 p.m. UTC | #1
I thought I'd CC'd Maged on this patch, but apparently not. I've
pushed it to trunk now.

On Fri, 21 Jan 2022 at 13:50, Jonathan Wakely wrote:
>
> Tested powerpc64le-linux. Does anybody see a problem with this change?
>
>
> The non-atomic store that sets both reference counts to zero uses a
> type-punned pointer, which has undefined behaviour. We could use memset
> to write 8 bytes, but we don't actually need it to be a single store
> anyway. No other thread can observe the values, that's why it's safe to
> use non-atomic stores in the first place. So we can just set each count
> to zero.
>
> With -fstore-merging (which is enabled by default at -O2) GCC produces
> the same code for this as for memset or the type punned store. Clang
> does that store merging even at -O1.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/shared_ptr_base.h (_Sp_counted_base<>::_M_release):
>         Set members to zero without type punning.
> ---
>  libstdc++-v3/include/bits/shared_ptr_base.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
> index 5b8f84b65be..b2f955b41f7 100644
> --- a/libstdc++-v3/include/bits/shared_ptr_base.h
> +++ b/libstdc++-v3/include/bits/shared_ptr_base.h
> @@ -340,7 +340,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>               // we are releasing the last strong reference. No other
>               // threads can observe the effects of this _M_release()
>               // call (e.g. calling use_count()) without a data race.
> -             *(long long*)(&_M_use_count) = 0;
> +             _M_weak_count = _M_use_count = 0;
>               _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
>               _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
>               _M_dispose();
> --
> 2.31.1
>
  
Maged Michael Jan. 24, 2022, 4:46 a.m. UTC | #2
Thanks Jonathan!

On Sun, Jan 23, 2022 at 5:51 PM Jonathan Wakely <jwakely@redhat.com> wrote:

> I thought I'd CC'd Maged on this patch, but apparently not. I've
> pushed it to trunk now.
>
> On Fri, 21 Jan 2022 at 13:50, Jonathan Wakely wrote:
> >
> > Tested powerpc64le-linux. Does anybody see a problem with this change?
> >
> >
> > The non-atomic store that sets both reference counts to zero uses a
> > type-punned pointer, which has undefined behaviour. We could use memset
> > to write 8 bytes, but we don't actually need it to be a single store
> > anyway. No other thread can observe the values, that's why it's safe to
> > use non-atomic stores in the first place. So we can just set each count
> > to zero.
> >
> > With -fstore-merging (which is enabled by default at -O2) GCC produces
> > the same code for this as for memset or the type punned store. Clang
> > does that store merging even at -O1.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/bits/shared_ptr_base.h
> (_Sp_counted_base<>::_M_release):
> >         Set members to zero without type punning.
> > ---
> >  libstdc++-v3/include/bits/shared_ptr_base.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h
> b/libstdc++-v3/include/bits/shared_ptr_base.h
> > index 5b8f84b65be..b2f955b41f7 100644
> > --- a/libstdc++-v3/include/bits/shared_ptr_base.h
> > +++ b/libstdc++-v3/include/bits/shared_ptr_base.h
> > @@ -340,7 +340,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >               // we are releasing the last strong reference. No other
> >               // threads can observe the effects of this _M_release()
> >               // call (e.g. calling use_count()) without a data race.
> > -             *(long long*)(&_M_use_count) = 0;
> > +             _M_weak_count = _M_use_count = 0;
> >               _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
> >               _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
> >               _M_dispose();
> > --
> > 2.31.1
> >
>
>
  

Patch

diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index 5b8f84b65be..b2f955b41f7 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -340,7 +340,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      // we are releasing the last strong reference. No other
 	      // threads can observe the effects of this _M_release()
 	      // call (e.g. calling use_count()) without a data race.
-	      *(long long*)(&_M_use_count) = 0;
+	      _M_weak_count = _M_use_count = 0;
 	      _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
 	      _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
 	      _M_dispose();