Why does GCC-11 segfault when including this particular set of standard libraries?

Viewed 565

Double Edit: GCC segfaults because of a probable bug and Clang segfaults on the same code only when using libstdc++. This was unknown to me at first because compiler segfaults are not particularly helpful, but with the flag -stdlib=libc++, you can get clang to compile this code just fine. The Clang segfault when using libstdc++ is a separate issue though, this one is focusing on GCC in particular.

The further reduced GCC example can be found attached to it's associated bug report #101184. If you are interested in a resolution, I would recommend keeping up with the bug report.


In the code wall below, there are a few small c++20 modules which cause a GCC-11 compiler segfault when built with the included commands.

I try to explain the issue in the text below, but the issue is a bit finicky and the best way I found to understand it is to play around with it a bit.

Two of these modules, "filler1" & "filler2" do not export anything and only include a couple standard libraries before the export module directive. Because nothing is actually being exported from these two, I would expect that it would not have any effect on the program behavior, but for some reason, if both are included, GCC segfaults. If either are removed, in any order, the program compiles fine. Also, the standard libraries included in these filler modules are particular, if "functional" or "memory" are removed, even though they are not used anywhere in the entire program, it compiles fine.

There is also the testmod1 module, which includes only "vector". But unlike the filler modules, for the segfault to occur, a vector of arbitrary type, which may or may not be exported, must be defined after the export module directive.

The final and possibly the most bizarre piece of the puzzle is the testmodp module. In addition to all the previous particulates being present, a template structure: template<typename... Ts> with a member vector: std::vectorstd::variant<Ts...> must be defined and then instantiated by the test_impl module. If I remove the template from the struct and change the member to: "std::vector<std::variant<int, float>> " It also works fine even though it is the same type as if it was instantiated by template types instead. Could anyone help me understand what is happening here?

Note: Because this error is so particular, a 5 file example is the smallest I could distill it to without behavior changing, Im also not allowed to include links so if you want to test it, copy everything with it's corrosponding name. Feel free to reach out in the comments if somthing is not right here (:

Thanks for reading!

FILE "filler1.cc"

module;
#include <memory>

export module filler1;

FILE "filler2.cc"

module;
#include <memory>
#include <functional>

export module filler2;

FILE "testmodp.cc"

module;
#include <functional>
#include <vector>
#include <variant>

export module testmodp;

export template<typename... Ts> // must be a template with args used in meta_variant for error to occur
struct testmodp{
    typedef std::variant<Ts...> meta_variant;
    std::vector<meta_variant> meta_gs;
};

FILE "testmod1.cc"

module;
#include <vector>

export module testmod1;

export struct testasdf{
    std::vector<int> testvec;
};

FILE "test_impl.cc"

module;
#include <memory>

import filler1;
import filler2;

import testmodp;
import testmod1;

export module test_impl;

export namespace test_impl {
    void test_init(){
        new testmodp<int,float>();
    }
};

GCC Commands to run

mkdir build
g++ -std=c++2a -fmodules-ts -c testmod1.cc -o build/testmod1.pcm
g++ -std=c++2a -fmodules-ts -c filler1.cc -o build/filler1.pcm
g++ -std=c++2a -fmodules-ts -c filler2.cc -o build/filler2.pcm
g++ -std=c++2a -fmodules-ts -c testmodp.cc -o build/testmodp.pcm
g++ -std=c++2a -fmodules-ts -c test_impl.cc -o build/test_impl.pcm

GCC internal compiler error:

module_test/test3/test_impl.cc:11:8: internal compiler error: in write_location, at cp/module.cc:15605
   11 | export module test_impl;
      |        ^~~~~~
0x1797368 internal_error(char const*, ...)
        ???:0
0x67f8f9 fancy_abort(char const*, int, char const*)
        ???:0
0x7652d9 trees_out::core_vals(tree_node*)
        ???:0
0x768e78 trees_out::tree_value(tree_node*)
        ???:0
0x761e9c trees_out::tree_node(tree_node*)
        ???:0
0x763cd6 trees_out::write_var_def(tree_node*)
        ???:0
0x764fd5 module_state::write_cluster(elf_out*, depset**, unsigned int, depset::hash&, unsigned int*, unsigned int*)
        ???:0
0x767ec9 module_state::write(elf_out*, cpp_reader*)
        ???:0
0x768d86 finish_module_processing(cpp_reader*)
        ???:0
0x713d1b c_parse_final_cleanups()
        ???:0
Please submit a full bug report,
Please include the complete backtrace with any bug report.
0 Answers
Related