Issue 95193
Summary [Clang 19.0.0] ICE when compiling code with `#pragma weak` and undefined function call
Labels clang
Assignees
Reporter iamanonymouscs
    ### Description:### 

Encountered an internal compiler error (ICE) while compiling a C code snippet using Clang version 19.0.0 on Ubuntu. 
Also ICE on trunk, compiler explorer:https:https://godbolt.org/z/P7hTMv8fK

```
====================================================
OS and Platform:
# uname -a
Linux ubuntu 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
====================================================
# clang -v
Ubuntu clang version 19.0.0 (++20240301064251+dd426fa5f931-1~exp1~20240301184412.1845)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Candidate multilib: .;@m64
Selected multilib: .;@m64
====================================================
Program:
# cat test.c

void doesntexist();
void myfunc() { int a =0; }
#pragma weak doesntexist=myfunc
int main() {
  doesntexist();
 return 0;
}

====================================================
```

Command Lines:
```bash
# clang test.c -O1 -Wall -Wextra -c -o test
/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c:2:21: warning: unused variable 'a' [-Wunused-variable]
    2 | void myfunc() { int a =0; }
      |                     ^
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: clang /root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c -O1 -Wall -Wextra -c -o /root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test
1. /root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c:5:15: current parser token ')'
2. /root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c:4:12: parsing function body 'main'
3. /root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c:4:12: in compound statement ('{}')
 #0 0x00007f0671af0216 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdc1216)
 #1 0x00007f0671aedec0 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdbeec0)
 #2 0x00007f0671aef5f4 llvm::sys::CleanupOnSignal(unsigned long) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdc05f4)
 #3 0x00007f0671a3d430 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xd0e430)
 #4 0x00007f067c4df980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007f0679e98ca2 clang::Sema::AddOverloadCandidate(clang::FunctionDecl*, clang::DeclAccessPair, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, bool, bool, bool, clang::CallExpr::ADLCallKind, llvm::MutableArrayRef<clang::ImplicitConversionSequence>, clang::OverloadCandidateParamOrder, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1a10ca2)
 #6 0x00007f0679bb8430 clang::Sema::DiagnoseEmptyLookup(clang::Scope*, clang::CXXScopeSpec&, clang::LookupResult&, clang::CorrectionCandidateCallback&, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::DeclContext*, clang::TypoExpr**) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1730430)
 #7 0x00007f0679eae5a1 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1a265a1)
 #8 0x00007f0679ead70f clang::Sema::BuildOverloadedCallExpr(clang::Scope*, clang::Expr*, clang::UnresolvedLookupExpr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1a2570f)
 #9 0x00007f0679bb17fd clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x17297fd)
#10 0x00007f0679bcaf6f clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1742f6f)
#11 0x00007f0679082b1a clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult<clang::Expr*, true>) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbfab1a)
#12 0x00007f06790870c8 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbff0c8)
#13 0x00007f067907fc2e clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbf7c2e)
#14 0x00007f067907fb09 clang::Parser::ParseExpression(clang::Parser::TypeCastState) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbf7b09)
#15 0x00007f06790e25d5 clang::Parser::ParseExprStatement(clang::Parser::ParsedStmtContext) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc5a5d5)
#16 0x00007f06790e08f6 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc588f6)
#17 0x00007f06790e02cb clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc582cb)
#18 0x00007f06790e8c80 clang::Parser::ParseCompoundStatementBody(bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc60c80)
#19 0x00007f06790e9def clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc61def)
#20 0x00007f0679103c36 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7bc36)
#21 0x00007f067904e3a7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbc63a7)
#22 0x00007f0679102cb4 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7acb4)
#23 0x00007f067910269b clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7a69b)
#24 0x00007f06791018ed clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc798ed)
#25 0x00007f06790ffd3c clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc77d3c)
#26 0x00007f067903811e clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbb011e)
#27 0x00007f067af35825 clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2aad825)
#28 0x00007f067aeb10d4 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2a290d4)
#29 0x00007f067afaef7e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2b26f7e)
#30 0x0000556350e0cfad cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x12fad)
#31 0x0000556350e0a075 (/usr/lib/llvm-19/bin/clang+0x10075)
#32 0x00007f067ab41439 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x26b9439)
#33 0x00007f0671a3d1dc llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xd0e1dc)
#34 0x00007f067ab40dfe clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x26b8dfe)
#35 0x00007f067ab08901 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2680901)
#36 0x00007f067ab08b4e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2680b4e)
#37 0x00007f067ab256cc clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x269d6cc)
#38 0x0000556350e099e5 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-19/bin/clang+0xf9e5)
#39 0x0000556350e17556 main (/usr/lib/llvm-19/bin/clang+0x1d556)
#40 0x00007f0670020c87 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c87)
#41 0x0000556350e06bfa _start (/usr/lib/llvm-19/bin/clang+0xcbfa)
clang: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Ubuntu clang version 19.0.0 (++20240301064251+dd426fa5f931-1~exp1~20240301184412.1845)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/test-3d2def.c
clang: note: diagnostic msg: /tmp/test-3d2def.sh
clang: note: diagnostic msg: 

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

#cat /tmp/test-3d2def.c
# 1 "<built-in>"
# 1 "/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c"
void doesntexist();
void myfunc() { int a =0; }
#pragma weak doesntexist=myfunc
int main() {
  doesntexist();
  return 0;
}

#cat /tmp/test-3d2def.sh
# Crash reproducer for Ubuntu clang version 19.0.0 (++20240301064251+dd426fa5f931-1~exp1~20240301184412.1845)
# Driver args: "/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c" "-O1" "-Wall" "-Wextra" "-c" "-o" "/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test"
# Original command:  "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.c" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=none" "-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=/root/gdbtest/gcctest/gcc_llvm/gcc/log/HasAttachment/fixed/109031" "-fcoverage-compilation-dir=/root/gdbtest/gcctest/gcc_llvm/gcc/log/HasAttachment/fixed/109031" "-resource-dir" "/usr/lib/llvm-19/lib/clang/19" "-internal-isystem" "/usr/lib/llvm-19/lib/clang/19/include" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-O1" "-Wall" "-Wextra" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o" "/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test" "-x" "c" "/root/gdbtest/gcctest/gcc_llvm/llvm/log/closed/HasCode/25449/test.c"
 "/usr/lib/llvm-19/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.c" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=none" "-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=/root/gdbtest/gcctest/gcc_llvm/gcc/log/HasAttachment/fixed/109031" "-fcoverage-compilation-dir=/root/gdbtest/gcctest/gcc_llvm/gcc/log/HasAttachment/fixed/109031" "-O1" "-Wall" "-Wextra" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-fcolor-diagnostics" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c" "test-3d2def.c"

```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to