public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-7954] c++: GC collects live data when synthesizing operator== [PR99831]
@ 2021-04-01 21:35 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2021-04-01 21:35 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6a60ffc297b9d4903543a25538e62e7fb39420a9

commit r11-7954-g6a60ffc297b9d4903543a25538e62e7fb39420a9
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Apr 1 10:42:43 2021 -0400

    c++: GC collects live data when synthesizing operator== [PR99831]
    
    Here we crash in reshape_init because we're accessing ggc_freed
    & poisoned data: since r277865 in defaulted_late_check we call
    synthesize_method here:
    
      if (kind == sfk_comparison)
        {
          /* If the function was declared constexpr, check that the definition
             qualifies.  Otherwise we can define the function lazily.  */
          if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
            synthesize_method (fn);
          return;
        }
    
    which in this test triggers when we're processing the string<"a">{} in
    the static_assert.  First, we create a CONSTRUCTOR for the "{}" in
    cp_parser_functional_cast, then we call finish_compound_literal which
    calls complete_type and that results in garbage collection, which then
    frees the CONSTRUCTOR {} we created when parsing the braced-list in
    string<"a">{} -- at this point, it's not referenced by anything.
    (That's not the case for 'type' in finish_compound_literal: the symbol
    table contains a node for operator==, so ggc_mark_roots goes and marks
    the fn decl, its type, its arguments etc., as used, so we don't collect
    it.)
    
    We could just bump function_depth around the new call to synthesize_method
    to prevent GC.
    
    gcc/cp/ChangeLog:
    
            PR c++/99831
            * method.c (defaulted_late_check): ++ and -- function_depth around
            the call to synthesize_method.
            * pt.c: Remove the saved_trees global.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/99831
            * g++.dg/other/gc6.C: New test.

Diff:
---
 gcc/cp/method.c                  |  7 ++++++-
 gcc/cp/pt.c                      |  1 -
 gcc/testsuite/g++.dg/other/gc6.C | 16 ++++++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 8ae7496f023..0f416bec35b 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -3131,7 +3131,12 @@ defaulted_late_check (tree fn)
       /* If the function was declared constexpr, check that the definition
 	 qualifies.  Otherwise we can define the function lazily.  */
       if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
-	synthesize_method (fn);
+	{
+	  /* Prevent GC.  */
+	  function_depth++;
+	  synthesize_method (fn);
+	  function_depth--;
+	}
       return;
     }
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index dc6f2f37f9b..7956e83c1de 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -65,7 +65,6 @@ static GTY(()) struct pending_template *last_pending_template;
 int processing_template_parmlist;
 static int template_header_count;
 
-static GTY(()) tree saved_trees;
 static vec<int> inline_parm_levels;
 
 static GTY(()) struct tinst_level *current_tinst_level;
diff --git a/gcc/testsuite/g++.dg/other/gc6.C b/gcc/testsuite/g++.dg/other/gc6.C
new file mode 100644
index 00000000000..ff45dd313d6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/gc6.C
@@ -0,0 +1,16 @@
+// PR c++/99831
+// { dg-do compile { target c++20 } }
+// { dg-options "--param ggc-min-heapsize=0 --param ggc-min-expand=0" }
+
+template <int N> struct S {
+  constexpr S(const char (&str)[N]) : value{} { }
+  char value[N];
+};
+template <S> struct string {
+  constexpr bool operator==(const string &) const = default;
+};
+template <S L2> void operator+(string<L2>) {
+  char value[1];
+  S{value};
+}
+static_assert(string<"a">{} == string<"a">{});


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-01 21:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 21:35 [gcc r11-7954] c++: GC collects live data when synthesizing operator== [PR99831] Marek Polacek

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).