Closed
Description
With the following code:
#include "clang/Frontend/ASTUnit.h"
#include "clang/Serialization/ASTWriter.h"
#include "clang/Serialization/InMemoryModuleCache.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Bitstream/BitstreamWriter.h"
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
int main() {
std::ifstream codeFile("input.cc");
std::stringstream code;
code << codeFile.rdbuf();
const std::unique_ptr<clang::ASTUnit> astUnit = clang::tooling::buildASTFromCode(code.str());
const bool hasErrors = false; // This will not cause a crash if this is true
llvm::SmallString<128> pchData;
llvm::BitstreamWriter pchDataStream(pchData);
clang::InMemoryModuleCache moduleCache;
clang::ASTWriter astWriter(pchDataStream, pchData, moduleCache, {}, false);
astWriter.WriteAST(astUnit->getSema(), "", nullptr, "", hasErrors);
std::ofstream file("input.gch", std::ios::binary | std::ios::out);
file << static_cast<std::string>(pchData);
}
and the below file:
// input.cc
int main() {
foo(FOO);
}
clang
will crash when using the input.gch
file generated from the code.
Output:
$ ./a.out
input.cc:3:6: error: use of undeclared identifier 'FOO'
foo(FOO);
^
$ clang input.gch
/tmp/input.cc:3:2: error: cannot compile this l-value expression yet
foo(FOO);
^~~
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /usr/bin/clang-13 -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name input.gch -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp -resource-dir /usr/lib/clang/13.0.1 -fdebug-compilation-dir=/tmp -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/input-11f595.o -x precompiled-header input.gch
1. <eof> parser at end of file
2. /tmp/input.cc:2:5: LLVM IR generation of declaration 'main'
3. /tmp/input.cc:2:5: Generating code for declaration 'main'
#0 0x00007fd23586cea7 (/usr/lib/libLLVM-13.so+0xba6ea7)
#1 0x00007fd23586a6a6 (/usr/lib/libLLVM-13.so+0xba46a6)
#2 0x00007fd234920da0 __restore_rt sigaction.c:0:0
#3 0x00007fd235a39b48 llvm::PointerType::get(llvm::Type*, unsigned int) (/usr/lib/libLLVM-13.so+0xd73b48)
#4 0x00007fd23cdec3bd clang::CodeGen::CodeGenFunction::EmitUnsupportedLValue(clang::Expr const*, char const*) (/usr/lib/libclang-cpp.so.13+0x180e3bd)
#5 0x00007fd23ce020e5 clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*) (/usr/lib/libclang-cpp.so.13+0x18240e5)
#6 0x00007fd23ce12222 clang::CodeGen::CodeGenFunction::EmitCallee(clang::Expr const*) (/usr/lib/libclang-cpp.so.13+0x1834222)
#7 0x00007fd23ce1266b clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/usr/lib/libclang-cpp.so.13+0x183466b)
#8 0x00007fd23ce1dbbe (/usr/lib/libclang-cpp.so.13+0x183fbbe)
#9 0x00007fd23ce57e52 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/usr/lib/libclang-cpp.so.13+0x1879e52)
#10 0x00007fd23ce11cdf clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/usr/lib/libclang-cpp.so.13+0x1833cdf)
#11 0x00007fd23ce11eb6 clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) (/usr/lib/libclang-cpp.so.13+0x1833eb6)
#12 0x00007fd23cf1f972 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/usr/lib/libclang-cpp.so.13+0x1941972)
#13 0x00007fd23cf20822 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/usr/lib/libclang-cpp.so.13+0x1942822)
#14 0x00007fd23cf78446 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/usr/lib/libclang-cpp.so.13+0x199a446)
#15 0x00007fd23cf98d40 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/usr/lib/libclang-cpp.so.13+0x19bad40)
#16 0x00007fd23cfa1d78 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/usr/lib/libclang-cpp.so.13+0x19c3d78)
#17 0x00007fd23cf9fb75 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/usr/lib/libclang-cpp.so.13+0x19c1b75)
#18 0x00007fd23cfc01bf (/usr/lib/libclang-cpp.so.13+0x19e21bf)
#19 0x00007fd23d00f5a4 (/usr/lib/libclang-cpp.so.13+0x1a315a4)
#20 0x00007fd23cf2c779 (/usr/lib/libclang-cpp.so.13+0x194e779)
#21 0x00007fd23d3de019 (/usr/lib/libclang-cpp.so.13+0x1e00019)
#22 0x00007fd23d3683ab non-virtual thunk to clang::ASTReader::StartTranslationUnit(clang::ASTConsumer*) (/usr/lib/libclang-cpp.so.13+0x1d8a3ab)
#23 0x00007fd23bfcc0f6 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/libclang-cpp.so.13+0x9ee0f6)
#24 0x00007fd23d5548b9 clang::FrontendAction::Execute() (/usr/lib/libclang-cpp.so.13+0x1f768b9)
#25 0x00007fd23d4fabbf clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/libclang-cpp.so.13+0x1f1cbbf)
#26 0x00007fd23d59ff20 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/libclang-cpp.so.13+0x1fc1f20)
#27 0x000055fe56ac784c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/bin/clang-13+0x1684c)
#28 0x000055fe56ac9c2d (/usr/bin/clang-13+0x18c2d)
#29 0x000055fe56abe185 main (/usr/bin/clang-13+0xd185)
#30 0x00007fd23490bb25 __libc_start_main (/usr/lib/libc.so.6+0x27b25)
#31 0x000055fe56ac048e _start (/usr/bin/clang-13+0xf48e)
clang-13: error: unable to execute command: Segmentation fault (core dumped)
clang-13: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 13.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang-13: note: diagnostic msg: Error generating preprocessed source(s) - no preprocessable inputs.