public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/vendors/redhat/heads/gcc-8-branch)] c++: Ensure correct destruction order of local statics [PR99613]
Date: Fri, 23 Apr 2021 10:54:29 +0000 (GMT)	[thread overview]
Message-ID: <20210423105429.894FD3A77C09@sourceware.org> (raw)

https://gcc.gnu.org/g:8830dc137e2593e3896291f185411c29072496cf

commit 8830dc137e2593e3896291f185411c29072496cf
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Mar 16 21:17:44 2021 +0100

    c++: Ensure correct destruction order of local statics [PR99613]
    
    As mentioned in the PR, if end of two constructions of local statics
    is strongly ordered, their destructors should be run in the reverse order.
    As we run __cxa_guard_release before calling __cxa_atexit, it is possible
    that we have two threads that access two local statics in the same order
    for the first time, one thread wins the __cxa_guard_acquire on the first
    one but is rescheduled in between the __cxa_guard_release and __cxa_atexit
    calls, then the other thread is scheduled and wins __cxa_guard_acquire
    on the second one and calls __cxa_quard_release and __cxa_atexit and only
    afterwards the first thread calls its __cxa_atexit.  This means a variable
    whose completion of the constructor strongly happened after the completion
    of the other one will be destructed after the other variable is destructed.
    
    The following patch fixes that by swapping the __cxa_guard_release and
    __cxa_atexit calls.
    
    2021-03-16  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/99613
            * decl.c (expand_static_init): For thread guards, call __cxa_atexit
            before calling __cxa_guard_release rather than after it.  Formatting
            fixes.
    
    (cherry picked from commit 1703937a05b8b95bc29d2de292387dfd9eb7c9a3)

Diff:
---
 gcc/cp/decl.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index a36692b18b1..5167151e67a 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -8397,17 +8397,25 @@ expand_static_init (tree decl, tree init)
 
 	  /* Do the initialization itself.  */
 	  init = add_stmt_to_compound (begin, init);
-	  init = add_stmt_to_compound
-	    (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
-	  init = add_stmt_to_compound
-	    (init, build_call_n (release_fn, 1, guard_addr));
+	  init = add_stmt_to_compound (init,
+				       build2 (MODIFY_EXPR, void_type_node,
+					       flag, boolean_true_node));
+
+	  /* Use atexit to register a function for destroying this static
+	     variable.  Do this before calling __cxa_guard_release.  */
+	  init = add_stmt_to_compound (init, register_dtor_fn (decl));
+
+	  init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
+							   guard_addr));
 	}
       else
-	init = add_stmt_to_compound (init, set_guard (guard));
+	{
+	  init = add_stmt_to_compound (init, set_guard (guard));
 
-      /* Use atexit to register a function for destroying this static
-	 variable.  */
-      init = add_stmt_to_compound (init, register_dtor_fn (decl));
+	  /* Use atexit to register a function for destroying this static
+	     variable.  */
+	  init = add_stmt_to_compound (init, register_dtor_fn (decl));
+	}
 
       finish_expr_stmt (init);


                 reply	other threads:[~2021-04-23 10:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210423105429.894FD3A77C09@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).