From patchwork Wed Sep 19 19:59:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ollie Wild X-Patchwork-Id: 185207 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id C14D22C0085 for ; Thu, 20 Sep 2012 05:59:25 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1348689566; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=1amKwVU LObdrbVUPiKr9oESENJU=; b=i4R/jvJ0c2ATpYMniLzB8VN0VHATCH+4piwK0WU OQ56UUwNwzM7kLTQL4bLlej8pthC8dJmu5tg/UG+U8l+vb1BvEYidH0BkZW7k5lK a/sYRfvhNP26QuOFi/J/sCdaPvCRHILgTTVGfd9jYS3mCcx9A5QQiy/zPL0qf8Cf BkyM= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Content-Type:X-System-Of-Record:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=s4AiYYooKbV40SW358CRI0ursuYJtupCoY4K1GgDeeaXfqgvX3pBLUlqTa9JAl ibkPQ9Zlx/sdo51VGgSX3VLZVGz1vgzx/K/0uS07v8pV3yD4OS7fPMIkaOS8vQmc NCCT2tUj7ceFpDc9CjMdXWWh1BahdEFlqLeztm+Bkasek=; Received: (qmail 10457 invoked by alias); 19 Sep 2012 19:59:20 -0000 Received: (qmail 10449 invoked by uid 22791); 19 Sep 2012 19:59:19 -0000 X-SWARE-Spam-Status: No, hits=-4.9 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, TW_CX X-Spam-Check-By: sourceware.org Received: from mail-vc0-f175.google.com (HELO mail-vc0-f175.google.com) (209.85.220.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 19 Sep 2012 19:59:06 +0000 Received: by vcqp1 with SMTP id p1so1934642vcq.20 for ; Wed, 19 Sep 2012 12:59:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type :x-system-of-record:x-gm-message-state; bh=H3hQu3KCvBjxwLsS501w6eWMRAChKmyFH5IN9T7Edys=; b=WTLTjHTMOnRLL3mtKJi+Yvr9TRttWgTwaTf5ibJcO4R1KLSd//u4d6Q+MN0ip9Evst hnaOmisZSqo9bCIZ6yCGFyqsrOKV9DMnBpkK9D31QDwTTFkl4KhMWbO7lsJX+coPpwpT FZ5JyYcBgq35oBHWYKBGGt2UYM+yIl6/tk6okwuEaqbizoMAnSNgDvh2U+8TU5Yb3XuJ vYMeihuS8JL8T2tX5f0empVLdKja2aQyPZlWXM7564JkllYKZ/bj4jf+Ae6ChXTaEBPL Kq8VhNjaMEoqJM+dK77oXCmLNnLH/g9x8fhUJ4LjLOHRk0rzKxQPVJnNycvlj8uanijS WVLw== Received: by 10.52.26.237 with SMTP id o13mr2120578vdg.44.1348084745956; Wed, 19 Sep 2012 12:59:05 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.26.237 with SMTP id o13mr2120569vdg.44.1348084745718; Wed, 19 Sep 2012 12:59:05 -0700 (PDT) Received: by 10.220.198.19 with HTTP; Wed, 19 Sep 2012 12:59:05 -0700 (PDT) Date: Wed, 19 Sep 2012 14:59:05 -0500 Message-ID: Subject: [google/gcc-4_7] fix race in __cxa_guard_acquire From: Ollie Wild To: gcc-patches , Paul Pluzhnikov X-System-Of-Record: true X-Gm-Message-State: ALoCoQmv2tcyBO+RkIfnRHQ4ZY2tpppE3K+FpzMXRguK4/dmq6s7JF1UXODbqJg/TzhGjFtyN9VcTJdXyzZXHwJepvA47YH5AZ2uHzZkK1EAFhxSCRZSlVox3ueKCcrZmFi1AlBkB1UVrnVLr5xxyr87StOs82t4MsAMG67pI4ositbr7fNB12Yq+9Pscn4jLLvXTam6wJkO X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This is a merge of r191125 and r191191 from gcc-4_7-branch. It fixes a race in __cxa_guard_acquire which can cause duplicate initialization of static variables within functions. Okay for google/gcc-4_7? Thanks, Ollie Google ref b/7173106. * libsupc++/guard.cc (__cxa_guard_acquire): Exit the loop earlier if we detect that another thread has had success. Don't compare_exchange from a finished state back to a waiting state. Fix up the last argument of the first __atomic_compare_exchange_n. Comment. commit 40ec687ace62b4d4c64f72be2bdf4321f5213107 Author: Ollie Wild Date: Wed Sep 19 14:52:53 2012 -0500 Merge r191125 and r191191 from gcc-4_7-branch. Google ref b/7173106. * libsupc++/guard.cc (__cxa_guard_acquire): Exit the loop earlier if we detect that another thread has had success. Don't compare_exchange from a finished state back to a waiting state. Fix up the last argument of the first __atomic_compare_exchange_n. Comment. diff --git a/libstdc++-v3/libsupc++/guard.cc b/libstdc++-v3/libsupc++/guard.cc index adc9608..f8550c0 100644 --- a/libstdc++-v3/libsupc++/guard.cc +++ b/libstdc++-v3/libsupc++/guard.cc @@ -244,16 +244,16 @@ namespace __cxxabiv1 if (__gthread_active_p ()) { int *gi = (int *) (void *) g; - int expected(0); const int guard_bit = _GLIBCXX_GUARD_BIT; const int pending_bit = _GLIBCXX_GUARD_PENDING_BIT; const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT; while (1) { + int expected(0); if (__atomic_compare_exchange_n(gi, &expected, pending_bit, false, __ATOMIC_ACQ_REL, - __ATOMIC_RELAXED)) + __ATOMIC_ACQUIRE)) { // This thread should do the initialization. return 1; @@ -264,13 +264,26 @@ namespace __cxxabiv1 // Already initialized. return 0; } + if (expected == pending_bit) { + // Use acquire here. int newv = expected | waiting_bit; if (!__atomic_compare_exchange_n(gi, &expected, newv, false, __ATOMIC_ACQ_REL, - __ATOMIC_RELAXED)) - continue; + __ATOMIC_ACQUIRE)) + { + if (expected == guard_bit) + { + // Make a thread that failed to set the + // waiting bit exit the function earlier, + // if it detects that another thread has + // successfully finished initialising. + return 0; + } + if (expected == 0) + continue; + } expected = newv; }