Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small mistake in -fmodule-file description breaks clang++-19 frontend with -fstack-protector #132059

Open
igormcoelho opened this issue Mar 19, 2025 · 9 comments
Labels
clang:modules C++20 modules and Clang Header Modules good first issue https://github.com/llvm/llvm-project/contribute

Comments

@igormcoelho
Copy link

igormcoelho commented Mar 19, 2025

I was experimenting CXX Modules with Clang 19 on Ubuntu, and it suddently broke when I passed wrong parameters on -fmodule-file, but strangely this only occurred when -fstack-protector was enabled... I have no idea why.

This is the clang version:

$ clang++-19 --version
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin

The files are:

// main.cc
import hello;

// import <string_view>;
import std;

int main() {
  do_hello("world");
  return 0;
}
// hello.cppm
export module hello;
import std;

export inline void do_hello(std::string_view const &name)
{
  std::cout << "Hello " << name << "!\n";
}

First I compile both .pcm files:

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o std.pcm /usr/lib/llvm-19/share/libc++/v1/std.cppm

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -fmodule-file=std=std.pcm -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o hello.pcm hello.cppm std.pcm

The bug appears when I mistakenly pass 'std' twice:

clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
$ clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-19/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -dumpdir main.pic.o- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -resource-dir /usr/lib/llvm-19/lib/clang/19 -internal-isystem /usr/lib/llvm-19/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fgnuc-version=4.2.1 -fno-implicit-modules -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-be446a.o -x c++ example/hello-world/main.cc
1.      example/hello-world/main.cc:5:11: current parser token ';'
 #0 0x000078607d8bd0af llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd0af)
 #1 0x000078607d8badb9 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebadb9)
 #2 0x000078607d8bd800 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd800)
 #3 0x000078607c245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #4 0x000078608711d3d1 clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&, unsigned int) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b1d3d1)
 #5 0x0000786087124c75 clang::ASTReader::ReadAST(llvm::StringRef, clang::serialization::ModuleKind, clang::SourceLocation, unsigned int, clang::serialization::ModuleFile**) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b24c75)
 #6 0x00007860872f0029 clang::CompilerInstance::findOrCompileModuleAndReadAST(llvm::StringRef, clang::SourceLocation, clang::SourceLocation, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf0029)
 #7 0x00007860872f1e94 clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation>>, clang::Module::NameVisibilityKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf1e94)
 #8 0x000078608610f1c1 clang::Sema::ActOnModuleImport(clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation>>, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x1b0f1c1)
 #9 0x00007860853200b9 clang::Parser::ParseModuleImport(clang::SourceLocation, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd200b9)
#10 0x000078608531ece7 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd1ece7)
#11 0x000078608524b44e clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xc4b44e)
#12 0x000078608737a68c clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2d7a68c)
#13 0x00007860872eda04 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2ceda04)
#14 0x00007860873f9d2c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2df9d2c)
#15 0x000061625d4a71f2 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x131f2)
#16 0x000061625d4a41c4 (/usr/lib/llvm-19/bin/clang+0x101c4)
#17 0x000061625d4a3362 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-19/bin/clang+0xf362)
#18 0x000061625d4b1723 main (/usr/lib/llvm-19/bin/clang+0x1d723)
#19 0x000078607c22a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#20 0x000078607c22a28b call_init ./csu/../csu/libc-start.c:128:20
#21 0x000078607c22a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#22 0x000061625d4a1dc5 _start (/usr/lib/llvm-19/bin/clang+0xddc5)
clang++-19: error: unable to execute command: Segmentation fault (core dumped)
clang++-19: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang++-19: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.cpp
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.sh
clang++-19: note: diagnostic msg: 

********************
$ cat /tmp/main-95dd6a.sh 
# Crash reproducer for Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
# Driver args: "--driver-mode=g++" "-fstack-protector" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-stdlib=libc++" "-std=c++23" "example/hello-world/main.cc" "-o" "main.pic.o"
# Original command:  "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-resource-dir" "/usr/lib/llvm-19/lib/clang/19" "-internal-isystem" "/usr/lib/llvm-19/bin/../include/c++/v1" "-internal-isystem" "/usr/lib/llvm-19/lib/clang/19/include" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o" "/tmp/main-be446a.o" "-x" "c++" "example/hello-world/main.cc"
 "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c++" "main-95dd6a.cpp"
$ cat /tmp/main-95dd6a.cpp
# 1 "example/hello-world/main.cc"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 484 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "example/hello-world/main.cc" 2

import hello;


import std;

int main() {
  do_hello("world");
  return 0;
}

It breaks with 1. example/hello-world/main.cc:5:11: current parser token ';'
But it seems to occur only if I enable -fstack-protector ... if I remove it, the bug is gone (but error correctly remains):

$ clang++-19  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world
/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
1 error generated.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 19, 2025
@EugeneZelenko EugeneZelenko added clang:modules C++20 modules and Clang Header Modules crash Prefer [crash-on-valid] or [crash-on-invalid] and removed clang Clang issues not falling into any other category labels Mar 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2025

@llvm/issue-subscribers-clang-modules

Author: Igor Machado Coelho (igormcoelho)

I was experimenting CXX Modules with Clang 19 on Ubuntu, and it suddently broke when I passed wrong parameters on -fmodule-file, but strangely this only occurred when -fstack-protector was enabled... I have no idea why.

This is the clang version:

$ clang++-19 --version
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin

The files are:

// main.cc
import hello;

// import &lt;string_view&gt;;
import std;

int main() {
  do_hello("world");
  return 0;
}
// hello.cppm
export module hello;
import std;

export inline void do_hello(std::string_view const &amp;name)
{
  std::cout &lt;&lt; "Hello " &lt;&lt; name &lt;&lt; "!\n";
}

First I compile both .pcm files:

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o std.pcm /usr/lib/llvm-19/share/libc++/v1/std.cppm

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -fmodule-file=std=std.pcm -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o hello.pcm hello.cppm std.pcm

The bug appears when I mistakenly pass 'std' twice:

clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
$ clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-19/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -dumpdir main.pic.o- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -resource-dir /usr/lib/llvm-19/lib/clang/19 -internal-isystem /usr/lib/llvm-19/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fgnuc-version=4.2.1 -fno-implicit-modules -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-be446a.o -x c++ example/hello-world/main.cc
1.      example/hello-world/main.cc:5:11: current parser token ';'
 #<!-- -->0 0x000078607d8bd0af llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd0af)
 #<!-- -->1 0x000078607d8badb9 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebadb9)
 #<!-- -->2 0x000078607d8bd800 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd800)
 #<!-- -->3 0x000078607c245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #<!-- -->4 0x000078608711d3d1 clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&amp;, unsigned int) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b1d3d1)
 #<!-- -->5 0x0000786087124c75 clang::ASTReader::ReadAST(llvm::StringRef, clang::serialization::ModuleKind, clang::SourceLocation, unsigned int, clang::serialization::ModuleFile**) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b24c75)
 #<!-- -->6 0x00007860872f0029 clang::CompilerInstance::findOrCompileModuleAndReadAST(llvm::StringRef, clang::SourceLocation, clang::SourceLocation, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf0029)
 #<!-- -->7 0x00007860872f1e94 clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef&lt;std::pair&lt;clang::IdentifierInfo*, clang::SourceLocation&gt;&gt;, clang::Module::NameVisibilityKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf1e94)
 #<!-- -->8 0x000078608610f1c1 clang::Sema::ActOnModuleImport(clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, llvm::ArrayRef&lt;std::pair&lt;clang::IdentifierInfo*, clang::SourceLocation&gt;&gt;, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x1b0f1c1)
 #<!-- -->9 0x00007860853200b9 clang::Parser::ParseModuleImport(clang::SourceLocation, clang::Sema::ModuleImportState&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd200b9)
#<!-- -->10 0x000078608531ece7 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd1ece7)
#<!-- -->11 0x000078608524b44e clang::ParseAST(clang::Sema&amp;, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xc4b44e)
#<!-- -->12 0x000078608737a68c clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2d7a68c)
#<!-- -->13 0x00007860872eda04 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2ceda04)
#<!-- -->14 0x00007860873f9d2c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2df9d2c)
#<!-- -->15 0x000061625d4a71f2 cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x131f2)
#<!-- -->16 0x000061625d4a41c4 (/usr/lib/llvm-19/bin/clang+0x101c4)
#<!-- -->17 0x000061625d4a3362 clang_main(int, char**, llvm::ToolContext const&amp;) (/usr/lib/llvm-19/bin/clang+0xf362)
#<!-- -->18 0x000061625d4b1723 main (/usr/lib/llvm-19/bin/clang+0x1d723)
#<!-- -->19 0x000078607c22a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#<!-- -->20 0x000078607c22a28b call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->21 0x000078607c22a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#<!-- -->22 0x000061625d4a1dc5 _start (/usr/lib/llvm-19/bin/clang+0xddc5)
clang++-19: error: unable to execute command: Segmentation fault (core dumped)
clang++-19: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang++-19: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.cpp
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.sh
clang++-19: note: diagnostic msg: 

********************
$ cat /tmp/main-95dd6a.sh 
# Crash reproducer for Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
# Driver args: "--driver-mode=g++" "-fstack-protector" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-stdlib=libc++" "-std=c++23" "example/hello-world/main.cc" "-o" "main.pic.o"
# Original command:  "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-resource-dir" "/usr/lib/llvm-19/lib/clang/19" "-internal-isystem" "/usr/lib/llvm-19/bin/../include/c++/v1" "-internal-isystem" "/usr/lib/llvm-19/lib/clang/19/include" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o" "/tmp/main-be446a.o" "-x" "c++" "example/hello-world/main.cc"
 "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c++" "main-95dd6a.cpp"
$ cat /tmp/main-95dd6a.cpp
# 1 "example/hello-world/main.cc"
# 1 "&lt;built-in&gt;" 1
# 1 "&lt;built-in&gt;" 3
# 484 "&lt;built-in&gt;" 3
# 1 "&lt;command line&gt;" 1
# 1 "&lt;built-in&gt;" 2
# 1 "example/hello-world/main.cc" 2

import hello;


import std;

int main() {
  do_hello("world");
  return 0;
}

It breaks with 1. example/hello-world/main.cc:5:11: current parser token ';'
But it seems to occur only if I enable -fstack-protector ... if I remove it, the bug is gone (but error correctly remains):

$ clang++-19  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world
/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
1 error generated.

@shafik
Copy link
Collaborator

shafik commented Mar 19, 2025

CC @ChuanqiXu9 @mpark

@ChuanqiXu9
Copy link
Member

The bug appears when I mistakenly pass 'std' twice:

clang++-19 -fstack-protector -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/h

What if you pass -fmodule-file=hello=... after that?

@igormcoelho
Copy link
Author

igormcoelho commented Mar 20, 2025

This is broken one with bug: clang++-19 -fstack-protector -fmodule-file=std=std.pcm -fmodule-file=std=hello.pcm -stdlib=libc++ -std=c++23 main.cc -o main.pic.o

What if you pass -fmodule-file=hello=... after that?

If I keep the buggy one and increment with that:

clang++-19 -fstack-protector -fmodule-file=std=std.pcm -fmodule-file=std=hello.pcm -fmodule-file=hello=hello.pcm -stdlib=libc++ -std=c++23 main.cc -o main.pic.o

Bug now changes! That's interesting...

 $ clang++-19 -fstack-protector  -fmodule-file=std=std.pcm -fmodule-file=std=hello.pcm -fmodule-file=hello=hello.pcm -stdlib=libc++ -std=c++23 main.cc -o main.pic.o 
main.cc:2:1: fatal error: malformed or corrupted AST file: 'refers to unknown module, cannot find std'
    2 | import hello;
      | ^
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-19/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -dumpdir main.pic.o- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cpp23_modules/example/hello-world -fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cpp23_modules/example/hello-world -resource-dir /usr/lib/llvm-19/lib/clang/19 -internal-isystem /usr/lib/llvm-19/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fgnuc-version=4.2.1 -fno-implicit-modules -fmodule-file=std=std.pcm -fmodule-file=std=hello.pcm -fmodule-file=hello=hello.pcm -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-4e753e.o -x c++ main.cc
1.      main.cc:2:13: current parser token ';'
 #0 0x000077f4bfabd0af llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd0af)
 #1 0x000077f4bfabadb9 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebadb9)
 #2 0x000077f4bfabd800 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd800)
 #3 0x000077f4be445330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #4 0x000077f4c931d3d1 clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&, unsigned int) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b1d3d1)
 #5 0x000077f4c9324c75 clang::ASTReader::ReadAST(llvm::StringRef, clang::serialization::ModuleKind, clang::SourceLocation, unsigned int, clang::serialization::ModuleFile**) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b24c75)
 #6 0x000077f4c94f0029 clang::CompilerInstance::findOrCompileModuleAndReadAST(llvm::StringRef, clang::SourceLocation, clang::SourceLocation, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf0029)
 #7 0x000077f4c94f1e94 clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation>>, clang::Module::NameVisibilityKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf1e94)
 #8 0x000077f4c830f1c1 clang::Sema::ActOnModuleImport(clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation>>, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x1b0f1c1)
 #9 0x000077f4c75200b9 clang::Parser::ParseModuleImport(clang::SourceLocation, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd200b9)
#10 0x000077f4c751ece7 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd1ece7)
#11 0x000077f4c751e876 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd1e876)
#12 0x000077f4c744b33b clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xc4b33b)
#13 0x000077f4c957a68c clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2d7a68c)
#14 0x000077f4c94eda04 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2ceda04)
#15 0x000077f4c95f9d2c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2df9d2c)
#16 0x000063dbbd9a81f2 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x131f2)
#17 0x000063dbbd9a51c4 (/usr/lib/llvm-19/bin/clang+0x101c4)
#18 0x000063dbbd9a4362 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-19/bin/clang+0xf362)
#19 0x000063dbbd9b2723 main (/usr/lib/llvm-19/bin/clang+0x1d723)
#20 0x000077f4be42a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#21 0x000077f4be42a28b call_init ./csu/../csu/libc-start.c:128:20
#22 0x000077f4be42a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#23 0x000063dbbd9a2dc5 _start (/usr/lib/llvm-19/bin/clang+0xddc5)
clang++-19: error: unable to execute command: Segmentation fault (core dumped)
clang++-19: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang++-19: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++-19: note: diagnostic msg: /tmp/main-3edfe7.cpp
clang++-19: note: diagnostic msg: /tmp/main-3edfe7.sh
clang++-19: note: diagnostic msg: 

********************

And if I try to inject more errors, like -fmodule-file=hello=std.pcm, then it GETS BETTER! hhahaah

$ clang++-19 -fstack-protector  -fmodule-file=std=std.pcm -fmodule-file=std=hello.pcm -fmodule-file=hello=hello.pcm -fmodule-file=hello=std.pcm -stdlib=libc++ 
-std=c++23 main.cc -o main.pic.o 
main.cc:2:8: fatal error: error in loading module 'hello' from prebuilt module path
    2 | import hello;
      |        ^
1 error generated.

And just for completeness, this really works fine with no errors:

$ clang++-19 -fstack-protector  -fmodule-file=std=std.pcm -fmodule-file=hello=hello.pcm -stdlib=libc++ -std=c++23 main.cc -o main.pic.o 

@ChuanqiXu9 ChuanqiXu9 added crash-on-invalid good first issue https://github.com/llvm/llvm-project/contribute and removed crash-on-invalid crash Prefer [crash-on-valid] or [crash-on-invalid] labels Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/issue-subscribers-good-first-issue

Author: Igor Machado Coelho (igormcoelho)

I was experimenting CXX Modules with Clang 19 on Ubuntu, and it suddently broke when I passed wrong parameters on -fmodule-file, but strangely this only occurred when -fstack-protector was enabled... I have no idea why.

This is the clang version:

$ clang++-19 --version
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin

The files are:

// main.cc
import hello;

// import &lt;string_view&gt;;
import std;

int main() {
  do_hello("world");
  return 0;
}
// hello.cppm
export module hello;
import std;

export inline void do_hello(std::string_view const &amp;name)
{
  std::cout &lt;&lt; "Hello " &lt;&lt; name &lt;&lt; "!\n";
}

First I compile both .pcm files:

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o std.pcm /usr/lib/llvm-19/share/libc++/v1/std.cppm

clang++-19 -std=c++23 -stdlib=libc++ -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++23' -fPIC -fmodule-file=std=std.pcm -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile -o hello.pcm hello.cppm std.pcm

The bug appears when I mistakenly pass 'std' twice:

clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
$ clang++-19 -fstack-protector  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-19/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -dumpdir main.pic.o- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module -resource-dir /usr/lib/llvm-19/lib/clang/19 -internal-isystem /usr/lib/llvm-19/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fgnuc-version=4.2.1 -fno-implicit-modules -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world/hello.pcm -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-be446a.o -x c++ example/hello-world/main.cc
1.      example/hello-world/main.cc:5:11: current parser token ';'
 #<!-- -->0 0x000078607d8bd0af llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd0af)
 #<!-- -->1 0x000078607d8badb9 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebadb9)
 #<!-- -->2 0x000078607d8bd800 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xebd800)
 #<!-- -->3 0x000078607c245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #<!-- -->4 0x000078608711d3d1 clang::ASTReader::ReadASTBlock(clang::serialization::ModuleFile&amp;, unsigned int) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b1d3d1)
 #<!-- -->5 0x0000786087124c75 clang::ASTReader::ReadAST(llvm::StringRef, clang::serialization::ModuleKind, clang::SourceLocation, unsigned int, clang::serialization::ModuleFile**) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2b24c75)
 #<!-- -->6 0x00007860872f0029 clang::CompilerInstance::findOrCompileModuleAndReadAST(llvm::StringRef, clang::SourceLocation, clang::SourceLocation, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf0029)
 #<!-- -->7 0x00007860872f1e94 clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef&lt;std::pair&lt;clang::IdentifierInfo*, clang::SourceLocation&gt;&gt;, clang::Module::NameVisibilityKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2cf1e94)
 #<!-- -->8 0x000078608610f1c1 clang::Sema::ActOnModuleImport(clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, llvm::ArrayRef&lt;std::pair&lt;clang::IdentifierInfo*, clang::SourceLocation&gt;&gt;, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x1b0f1c1)
 #<!-- -->9 0x00007860853200b9 clang::Parser::ParseModuleImport(clang::SourceLocation, clang::Sema::ModuleImportState&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd200b9)
#<!-- -->10 0x000078608531ece7 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xd1ece7)
#<!-- -->11 0x000078608524b44e clang::ParseAST(clang::Sema&amp;, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0xc4b44e)
#<!-- -->12 0x000078608737a68c clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2d7a68c)
#<!-- -->13 0x00007860872eda04 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2ceda04)
#<!-- -->14 0x00007860873f9d2c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x2df9d2c)
#<!-- -->15 0x000061625d4a71f2 cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x131f2)
#<!-- -->16 0x000061625d4a41c4 (/usr/lib/llvm-19/bin/clang+0x101c4)
#<!-- -->17 0x000061625d4a3362 clang_main(int, char**, llvm::ToolContext const&amp;) (/usr/lib/llvm-19/bin/clang+0xf362)
#<!-- -->18 0x000061625d4b1723 main (/usr/lib/llvm-19/bin/clang+0x1d723)
#<!-- -->19 0x000078607c22a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#<!-- -->20 0x000078607c22a28b call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->21 0x000078607c22a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#<!-- -->22 0x000061625d4a1dc5 _start (/usr/lib/llvm-19/bin/clang+0xddc5)
clang++-19: error: unable to execute command: Segmentation fault (core dumped)
clang++-19: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang++-19: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.cpp
clang++-19: note: diagnostic msg: /tmp/main-95dd6a.sh
clang++-19: note: diagnostic msg: 

********************
$ cat /tmp/main-95dd6a.sh 
# Crash reproducer for Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2)
# Driver args: "--driver-mode=g++" "-fstack-protector" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-stdlib=libc++" "-std=c++23" "example/hello-world/main.cc" "-o" "main.pic.o"
# Original command:  "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-resource-dir" "/usr/lib/llvm-19/lib/clang/19" "-internal-isystem" "/usr/lib/llvm-19/bin/../include/c++/v1" "-internal-isystem" "/usr/lib/llvm-19/lib/clang/19/include" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o" "/tmp/main-be446a.o" "-x" "c++" "example/hello-world/main.cc"
 "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-dumpdir" "main.pic.o-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cc" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-fcoverage-compilation-dir=/home/imcoelho/git-reps/rules_cc_module" "-std=c++23" "-fdeprecated-macro" "-ferror-limit" "19" "-stack-protector" "1" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fmodule-file=std=example/hello-world/std.pcm" "-fmodule-file=std=example/hello-world/hello.pcm" "-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c++" "main-95dd6a.cpp"
$ cat /tmp/main-95dd6a.cpp
# 1 "example/hello-world/main.cc"
# 1 "&lt;built-in&gt;" 1
# 1 "&lt;built-in&gt;" 3
# 484 "&lt;built-in&gt;" 3
# 1 "&lt;command line&gt;" 1
# 1 "&lt;built-in&gt;" 2
# 1 "example/hello-world/main.cc" 2

import hello;


import std;

int main() {
  do_hello("world");
  return 0;
}

It breaks with 1. example/hello-world/main.cc:5:11: current parser token ';'
But it seems to occur only if I enable -fstack-protector ... if I remove it, the bug is gone (but error correctly remains):

$ clang++-19  -fmodule-file=std=example/hello-world/std.pcm -fmodule-file=std=example/hello-world
/hello.pcm -stdlib=libc++ -std=c++23 example/hello-world/main.cc -o main.pic.o 
example/hello-world/main.cc:2:8: fatal error: module 'hello' not found
    2 | import hello;
      | ~~~~~~~^~~~~
1 error generated.

@ChuanqiXu9
Copy link
Member

I am relatively busy right now. And this looks like a simple check, diagnose and return pattern to fix this. So I marked it as good-first-issue now.

@naveen-seth
Copy link

naveen-seth commented Mar 24, 2025

I have reproduced the crashes using a Debug build of Clang and it is always caused by failing this assert / the following index into MF.TransitiveImports:

https://github.com/llvm/llvm-project/blob/943a70717c629f43b309ab56e8141ffb131871a6/clang/lib/Serialization/ASTReader.cpp#L953C2-L956C78

I was also able to reproduce the crashes without -fstack-protector or duplicate command line arguments. The issue seems to only depend on giving a bad argument to -fmodule-file in combination with transitive imports.

// hello.cppm
export module hello;
import std;

export inline void do_hello(std::string_view const &name)
{
  std::cout << "Hello " << name << "!\n";
}
// main.cpp
import std;

int main() {
  std::cout << "Hello, World!\n";
  return 0;
}
clang++ -std=c++20 -stdlib=libc++ --precompile /usr/lib/llvm-19/share/libc++/v1/std.cppm -o std.pcm

clang++ -std=c++20 -stdlib=libc++ --precompile hello.cppm -o hello.pcm -fmodule-file=std=std.pcm

clang++ -v -std=c++20 -stdlib=libc++ main.cpp -fmodule-file=std=hello.pcm

Full crash report with diagnostic messages appended:
crash-report.txt

I'd really like to try fixing this. Can I take this issue?

@mpark
Copy link
Member

mpark commented Mar 24, 2025

@naveen-seth I say go for it!

naveen-seth added a commit to naveen-seth/llvm-project-fmodule-file-crash-bug that referenced this issue Mar 28, 2025
…llvm#132059)

When using -fmodule-file=<name>=<path/to/bmi> with incorrect inputs,
the compiler crashes in two scenarios:

1. A module is mapped to the right BMI file but one of its
transitively exported module dependencies is also mapped to the same
BMI file.
2. A module is mapped to a wrong BMI file which also transitively
exports that module.

The crash is caused during serialization, when trying to resolve
declaration IDs in the AST body after having imported the wrong
module.

Because the 2nd scenario can only be detected after reading the BMI's
module name, checking for duplicate values while parsing command-line
options is not enough to fix the crash.

This commit fixes the issue by validating module identity after having
read the AST's ControlBlock.
naveen-seth added a commit to naveen-seth/llvm-project-fmodule-file-crash-bug that referenced this issue Mar 28, 2025
…llvm#132059)

When using -fmodule-file=<name>=<path/to/bmi> with incorrect inputs,
the compiler crashes in two scenarios:

1. A module is mapped to the right BMI file but one of its
transitively exported module dependencies is also mapped to the same
BMI file.
2. A module is mapped to a wrong BMI file which also transitively
exports that module.

The crash is caused during serialization, when trying to resolve
declaration IDs in the AST body after having imported the wrong
module.

Because the 2nd scenario can only be detected after reading the BMI's
module name, checking for duplicate values while parsing command-line
options is not enough to fix the crash.

This commit fixes the issue by validating module identity after having
read the AST's ControlBlock.
naveen-seth added a commit to naveen-seth/llvm-project-fmodule-file-crash-bug that referenced this issue Mar 28, 2025
…llvm#132059)

When using -fmodule-file=<name>=<path/to/bmi> with incorrect inputs,
the compiler crashes in two scenarios:

1. A module is mapped to the right BMI file but one of its
transitively exported module dependencies is also mapped to the same
BMI file.
2. A module is mapped to a wrong BMI file which also transitively
exports that module.

The crash is caused during serialization, when trying to resolve
declaration IDs in the AST body after having imported the wrong
module.

Because the 2nd scenario can only be detected after reading the BMI's
module name, checking for duplicate values while parsing command-line
options is not enough to fix the crash.

This commit fixes the issue by validating module identity after having
read the AST's ControlBlock.
naveen-seth added a commit to naveen-seth/llvm-project-fmodule-file-crash-bug that referenced this issue Mar 28, 2025
When using -fmodule-file=<name>=<path/to/bmi> with incorrect inputs,
the compiler crashes in two scenarios:

1. A module is mapped to the right BMI file but one of its
transitively exported module dependencies is also mapped to the same
BMI file.
2. A module is mapped to a wrong BMI file which also transitively
exports that module.

The crash is caused during serialization, when trying to resolve
declaration IDs in the AST body after having imported the wrong
module.

Because the 2nd scenario can only be detected after reading the BMI's
module name, checking for duplicate values while parsing command-line
options is not enough to fix the crash.

This commit fixes the issue by validating module identity after having
read the AST's ControlBlock.
naveen-seth added a commit to naveen-seth/llvm-project-fmodule-file-crash-bug that referenced this issue Mar 28, 2025
When using -fmodule-file=<name>=<path/to/bmi> with incorrect inputs,
the compiler crashes in two scenarios:

1. A module is mapped to the right BMI file but one of its
transitively exported module dependencies is also mapped to the same
BMI file.
2. A module is mapped to a wrong BMI file which also transitively
exports that module.

The crash is caused during serialization, when trying to resolve
declaration IDs in the AST body after having imported the wrong
module.

Because the 2nd scenario can only be detected after reading the BMI's
module name, checking for duplicate values while parsing command-line
options is not enough to fix the crash.

This commit fixes the issue by validating module identity after having
read the AST's ControlBlock.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

No branches or pull requests

7 participants