public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ppalka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/99426] [modules] failed to read compiled module cluster 1186: Bad file data
Date: Tue, 27 Feb 2024 02:15:18 +0000	[thread overview]
Message-ID: <bug-99426-4-GUqih5SOUG@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-99426-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99426

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org
   Last reconfirmed|2021-03-30 00:00:00         |2024-2-21

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
I've observed a similar error on trunk when compiling our xtreme-header
testcase without -fno-module-lazy:

$ g++ -fmodules-ts -std=c++20
gcc/testsuite/g++.dg/modules/xtreme-header_{a.H,b.C}
In module imported at gcc/testsuite/g++.dg/modules/xtreme-header_b.C:4:1:
./gcc/testsuite/g++.dg/modules/xtreme-header_a.H: At global scope:
./gcc/testsuite/g++.dg/modules/xtreme-header_a.H: error: failed to read
compiled module cluster 5541: Bad file data
./gcc/testsuite/g++.dg/modules/xtreme-header_a.H: note: compiled module file is
‘gcm.cache/,/gcc/testsuite/g++.dg/modules/xtreme-header_a.H.gcm’
In file included from
/scratchpad/gcc-build-prefix/include/c++/14.0.1/string:54,
                 from
/scratchpad/gcc-build-prefix/include/c++/14.0.1/bitset:52,
                 from gcc/testsuite/g++.dg/modules/xtreme-header.h:9:
/scratchpad/gcc-build-prefix/include/c++/14.0.1/bits/basic_string.h:4249:33:
fatal error: failed to load pendings for ‘std::__cxx11::basic_string’

Although this manifests as a serialization error, it's really a GC issue: a
streamed-in local class from entity_ary (which is not a GC root) gets
prematurely GC'd (since it's only reachable from entity_ary), and later when
resolving a reference to this GC'd local class the tt_entity case of
trees_in::tree_node fails.

In order to achieve a small reproducer, it seems setting
--param=ggc-min-heapsize/expand=0 is not enough, we need to add more collection
points:

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2803824d11e..44c205b2529 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -27367,6 +27367,7 @@ instantiate_pending_templates (int retries)
        {
          tree instantiation = reopen_tinst_level ((*t)->tinst);
          bool complete = false;
+         ggc_collect (GGC_COLLECT_FORCE);

          if (TYPE_P (instantiation))
            {

With this patch to force GC after every instantiation, the following small
testcase can reproduce the issue:

$ cat xtreme-header.ii
struct string {
  template <typename T> void g(T) {
    struct _Local { };
  }
};

template <typename _Tp> void f(_Tp) { }

inline void foo() {
  string s;
  f(s);
  s.g(0);
}

$ cat xtreme-header_a.H
#include "xtreme-header.ii"

$ cat xtreme-header_b.C
#include "xtreme-header.ii"
import "xtreme-header_a.H";

$ g++ -fmodules-ts xtreme_header_{a.H,b.C}
./xtreme-header_a.H: error: failed to read compiled module cluster 4: Bad file
data
./xtreme-header_a.H: note: compiled module file is
‘gcm.cache/,/xtreme-header_a.H.gcm’
In file included from xtreme-header_b.C:1:
xtreme-header.ii:12:6: fatal error: failed to load pendings for ‘::string’

Another way to observe this GC issue by forcing a GC right before we clear
entity_ary and checking if any trees within entity_ary have been freed:

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 106af7bdb3e..a6edc9b033a 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -20460,6 +20460,12 @@ fini_modules (cpp_reader *reader, void *cookie, bool
has_inits)
   /* No need to lookup modules anymore.  */
   modules_hash = NULL;

+  ggc_collect (GGC_COLLECT_FORCE);
+  if (entity_ary)
+    for (binding_slot& t : *entity_ary)
+      if (!t.is_lazy () && (tree)t && TREE_CODE ((tree)t) == 0xa5a5)
+       printf ("XXX\n");
+
   /* Or entity array.  We still need the entity map to find import numbers. 
*/
   vec_free (entity_ary);
   entity_ary = NULL;

With that the following minimal testcase demonstrates the GC issue:

$ cat xtreme-header.ii
template <typename> void _M_construct() { struct A { }; }

$ g++ -fmodules-ts xtreme-header_{a.H,b.C}
XXX

  parent reply	other threads:[~2024-02-27  2:15 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-06  7:54 [Bug c++/99426] New: " alexander.lelyakin at googlemail dot com
2021-03-09 15:56 ` [Bug c++/99426] " alexander.lelyakin at googlemail dot com
2021-03-12  5:40 ` alexander.lelyakin at googlemail dot com
2021-03-24 19:38 ` alexander.lelyakin at googlemail dot com
2021-03-30  1:34 ` mpolacek at gcc dot gnu.org
2021-03-30  6:50 ` alexander.lelyakin at googlemail dot com
2022-09-08  4:39 ` markmigm at gmail dot com
2024-02-27  2:15 ` ppalka at gcc dot gnu.org [this message]
2024-02-29 18:04 ` ppalka at gcc dot gnu.org
2024-04-03 21:19 ` ppalka at gcc dot gnu.org
2024-04-07 12:33 ` nickbegg at gmail dot com
2024-04-07 14:56 ` ppalka at gcc dot gnu.org
2024-04-07 20:49 ` nickbegg at gmail dot com
2024-04-12 19:52 ` ppalka at gcc dot gnu.org
2024-04-12 19:53 ` ppalka at gcc dot gnu.org

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=bug-99426-4-GUqih5SOUG@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).