From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30295 invoked by alias); 19 Feb 2014 12:27:48 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 30239 invoked by uid 48); 19 Feb 2014 12:27:45 -0000 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/60272] atomic<>::compare_exchange_weak has spurious store and can cause race conditions Date: Wed, 19 Feb 2014 12:27:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.8.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW 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: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-02/txt/msg01961.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60272 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org, | |rth at gcc dot gnu.org, | |torvald at gcc dot gnu.org --- Comment #1 from Jakub Jelinek --- Even our __atomic_compare_exchange* documentation states that: If they are not equal, the current contents of @code{*@var{ptr}} is written into @code{*@var{expected}}. But then expand_builtin_atomic_compare_exchange doesn't care: oldval = expect; if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target), &oldval, mem, oldval, desired, is_weak, success, failure)) return NULL_RTX; if (oldval != expect) emit_move_insn (expect, oldval); That effectively means that expect will be stored unconditionally. So, either we'd need to change this function, so that it sets oldval to NULL_RTX first, and passes ..., &oldval, mem, expected, ... and needs to also always ask for target, then conditionally on target store to expected, or perhaps add extra parameter to expand_atomic_compare_and_swap and do the store only conditionally in that case. Richard/Torvald?