From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C646B3857824; Thu, 11 Mar 2021 02:00:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C646B3857824 From: "lewissbaker.opensource at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/99537] New: Wrong memory_order used in stop_token ref-counting Date: Thu, 11 Mar 2021 02:00:53 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lewissbaker.opensource at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Mar 2021 02:00:53 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99537 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) =3D=3D 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 memor= y.=