public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/99537] New: Wrong memory_order used in stop_token ref-counting
@ 2021-03-11  2:00 lewissbaker.opensource at gmail dot com
  2021-03-11 13:34 ` [Bug libstdc++/99537] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: lewissbaker.opensource at gmail dot com @ 2021-03-11  2:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99537

            Bug ID: 99537
           Summary: Wrong memory_order used in stop_token ref-counting
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lewissbaker.opensource at gmail dot com
  Target Milestone: ---

In the implementation of stop_token the _Stop_state_t implements
reference-counting for tracking shared ownership of the stop-state.

This is done via two methods:

      void
      _M_add_owner() noexcept
      {
        _M_owners.fetch_add(1, memory_order::relaxed);
      }

      void
      _M_release_ownership() noexcept
      {
        if (_M_owners.fetch_sub(1, memory_order::release) == 1)
          delete this;
      }

Other than initialising the _M_owners atomic value to 1, these are the only two
accesses of the _M_owners variable.

The 'fetch_sub()' operation in _M_release_ownership() should be using
memory_order::acq_rel instead of memory_order::release. The use of 'release'
only is insufficient as it does not synchronise with any corresponding
'acquire' operation.

With the current implementation, it's possible that a prior write to one of the
_M_value or _M_head data-members by a thread releasing the second-to-last
reference might not be visible to another thread that releases the last
reference and frees the memory, resulting in potential write to freed memory.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-03-29 21:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11  2:00 [Bug libstdc++/99537] New: Wrong memory_order used in stop_token ref-counting lewissbaker.opensource at gmail dot com
2021-03-11 13:34 ` [Bug libstdc++/99537] " redi at gcc dot gnu.org
2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
2021-03-29 20:03 ` cvs-commit at gcc dot gnu.org
2021-03-29 21:36 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).