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

clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType): Assertion `Init->containsErrors() && "Dependent code should only occur in error-recovery path."' failed. #50320

Closed
chengniansun opened this issue Jul 4, 2021 · 6 comments
Labels
bug Indicates an unexpected problem or unintended behavior bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party crash-on-invalid

Comments

@chengniansun
Copy link

chengniansun commented Jul 4, 2021

Bugzilla Link 50976
Version trunk
OS Linux
CC @zygoloid

Extended Description

$ clang-trunk -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git 6b0d266)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cnsun/usr/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@MX32
Selected multilib: .;@m64

$ cat mutant.c

a;
const b;
c[] = {b = a} static d = c

$ clang-trunk -w mutant.c
mutant.c:3:10: error: cannot assign to variable 'b' with const-qualified type 'const int'
c[] = {b = a} static d = c
~ ^
mutant.c:2:7: note: variable 'b' declared const here
const b;

mutant.c:3:14: error: expected ';' after top level declarator
c[] = {b = a} static d = c
             ^
             ;
clang-13: /tmp/tmp.OKn6kNiY8c-clang-builder/llvm-project/clang/lib/Sema/SemaDecl.cpp:11350: bool clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType): Assertion `Init->containsErrors() && "Dependent code should only occur in error-recovery path."' failed.
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: /scratch/software/clang-trunk/bin/clang-13 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -main-file-name mutant.c -mrelocation-model static -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=/scratch/cnsun/workspace/perses-fuzzer/default_c_finding_folder/crash_20210702_072052_024a/delta/perses_result -resource-dir /scratch/software/clang-trunk/lib/clang/13.0.0 -c-isystem . -c-isystem /usr/local/include/cmsith -internal-isystem /scratch/software/clang-trunk/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -w -fdebug-compilation-dir=/scratch/cnsun/workspace/perses-fuzzer/default_c_finding_folder/crash_20210702_072052_024a/delta/perses_result -ferror-limit 19 -fgnuc-version=4.2.1 -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/mutant-dee107.o -x c mutant.c
1.	<eof> parser at end of file
 #&#8203;0 0x0000565476f3f714 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #&#8203;1 0x0000565476f3cece SignalHandler(int) Signals.cpp:0:0
 #&#8203;2 0x00007fbe28c003c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
 #&#8203;3 0x00007fbe2869f18b raise /build/glibc-ZN95T4/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #&#8203;4 0x00007fbe2867e859 abort /build/glibc-ZN95T4/glibc-2.31/stdlib/abort.c:81:7
 #&#8203;5 0x00007fbe2867e729 get_sysdep_segment_value /build/glibc-ZN95T4/glibc-2.31/intl/loadmsgcat.c:509:8
 #&#8203;6 0x00007fbe2867e729 _nl_load_domain /build/glibc-ZN95T4/glibc-2.31/intl/loadmsgcat.c:970:34
 #&#8203;7 0x00007fbe2868ff36 (/lib/x86_64-linux-gnu/libc.so.6+0x36f36)
 #&#8203;8 0x00005654793ef72d clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType) (/scratch/software/clang-trunk/bin/clang-13+0x5e4572d)
 #&#8203;9 0x00005654794371d4 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/scratch/software/clang-trunk/bin/clang-13+0x5e8d1d4)
#&#8203;10 0x000056547918c666 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/scratch/software/clang-trunk/bin/clang-13+0x5be2666)
#&#8203;11 0x000056547919f54b clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/scratch/software/clang-trunk/bin/clang-13+0x5bf554b)
#&#8203;12 0x000056547916d636 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/scratch/software/clang-trunk/bin/clang-13+0x5bc3636)
#&#8203;13 0x000056547916dc21 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.0) Parser.cpp:0:0
#&#8203;14 0x0000565479174447 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (/scratch/software/clang-trunk/bin/clang-13+0x5bca447)
#&#8203;15 0x00005654791759de clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, bool) (/scratch/software/clang-trunk/bin/clang-13+0x5bcb9de)
#&#8203;16 0x0000565479168209 clang::ParseAST(clang::Sema&, bool, bool) (/scratch/software/clang-trunk/bin/clang-13+0x5bbe209)
#&#8203;17 0x00005654780af2a8 clang::CodeGenAction::ExecuteAction() (/scratch/software/clang-trunk/bin/clang-13+0x4b052a8)
#&#8203;18 0x0000565477952399 clang::FrontendAction::Execute() (/scratch/software/clang-trunk/bin/clang-13+0x43a8399)
#&#8203;19 0x00005654778e53b6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/scratch/software/clang-trunk/bin/clang-13+0x433b3b6)
#&#8203;20 0x0000565477a300c0 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/scratch/software/clang-trunk/bin/clang-13+0x44860c0)
#&#8203;21 0x0000565474822846 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/scratch/software/clang-trunk/bin/clang-13+0x1278846)
#&#8203;22 0x000056547481eb88 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#&#8203;23 0x000056547474c156 main (/scratch/software/clang-trunk/bin/clang-13+0x11a2156)
#&#8203;24 0x00007fbe286800b3 __libc_start_main /build/glibc-ZN95T4/glibc-2.31/csu/../csu/libc-start.c:342:3
#&#8203;25 0x000056547481e6fe _start (/scratch/software/clang-trunk/bin/clang-13+0x12746fe)
clang-13: error: unable to execute command: Aborted (core dumped)
clang-13: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 13.0.0 (https://github.com/llvm/llvm-project.git 6b0d266036f73f5ee9556d211a7d0946091ff3b2)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cnsun/usr/bin
clang-13: note: diagnostic msg:
********************

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

********************
@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@ramosian-glider
Copy link
Contributor

This problem is still reproducible on trunk Clang.
I have yet another repro for it:

$ cat k_t.i
struct kunit_case {};
struct kunit_suite {};
static void test_memset16() {}
static void test_memset16() {}
static struct kunit_case kmsan_test_cases[] = {.run_case = test_memset16};
static struct kunit_suite kmsan_test_suite = {
  .test_cases = kmsan_test_cases
};
$ clang  -c k_t.i -w
...
clang: /usr/local/google/src/llvm-git-monorepo/clang/lib/Sema/SemaDecl.cpp:12203: bool clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType): Assertion `Init->containsErrors() && "Dependent code should only occur in error-recovery path."' failed.

@ramosian-glider ramosian-glider added bug Indicates an unexpected problem or unintended behavior clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 1, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 1, 2023

@llvm/issue-subscribers-bug

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 1, 2023

@llvm/issue-subscribers-clang-frontend

@shafik
Copy link
Collaborator

shafik commented Mar 1, 2023

Confirmed: https://godbolt.org/z/3odYPn4Gn

Assertion:

clang++: /root/llvm-project/clang/lib/Sema/SemaDecl.cpp:12203:
bool clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType):
Assertion `Init->containsErrors() && "Dependent code should only occur in error-recovery path."' failed.

Backtrace:

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: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -x c <source>
1.	<source>:8:2: current parser token ';'
 #0 0x000055cf14b3380f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x407280f)
 #1 0x000055cf14b3154c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x407054c)
 #2 0x000055cf14a7eef8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f62f96e0420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007f62f91ad00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #5 0x00007f62f918c859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #6 0x00007f62f918c729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #7 0x00007f62f919dfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #8 0x000055cf174fba7d clang::Sema::CheckForConstantInitializer(clang::Expr*, clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a3aa7d)
 #9 0x000055cf17549388 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a88388)
#10 0x000055cf1721ae6e clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6759e6e)
#11 0x000055cf1722c2f8 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x676b2f8)
#12 0x000055cf171f4d22 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6733d22)
#13 0x000055cf171f55cf clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.0) Parser.cpp:0:0
#14 0x000055cf171fc0a9 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x673b0a9)
#15 0x000055cf171fc9fd clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x673b9fd)
#16 0x000055cf171f089a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x672f89a)
#17 0x000055cf15e94e98 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x53d3e98)
#18 0x000055cf156fdbc9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c3cbc9)
#19 0x000055cf15681b56 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bc0b56)
#20 0x000055cf157e1507 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4d20507)
#21 0x000055cf12027626 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1566626)
#22 0x000055cf1202313a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#23 0x000055cf154e8e9d void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#24 0x000055cf14a7f3e0 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3fbe3e0)
#25 0x000055cf154e975f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#26 0x000055cf154b146c clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49f046c)
#27 0x000055cf154b1f0d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49f0f0d)
#28 0x000055cf154b9bdd clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49f8bdd)
#29 0x000055cf120259dc clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x15649dc)
#30 0x000055cf11f32465 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1471465)
#31 0x00007f62f918e083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#32 0x000055cf1201dd6e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x155cd6e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

@shafik shafik added confirmed Verified by a second party crash-on-invalid and removed clang Clang issues not falling into any other category labels Mar 1, 2023
@DimitryAndric
Copy link
Collaborator

See also https://bugs.freebsd.org/272415 (it's pretty certain that's the same assertion failure).

@shafik
Copy link
Collaborator

shafik commented Jul 7, 2023

In Sema::AddInitializerToDecl(...) if we check Init->containsErrors() it is true it does not looks like we ever check this in Parsing. So I think we need to handle it here.

here

InitializationSequence InitSeq(*this, Entity, Kind, Args,
/*TopLevelOfInitList=*/false,
/*TreatUnavailableAsInvalid=*/false);
ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT);
if (Result.isInvalid()) {
// If the provided initializer fails to initialize the var decl,
// we attach a recovery expr for better recovery.
auto RecoveryExpr =
CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), Args);
if (RecoveryExpr.get())
VDecl->setInit(RecoveryExpr.get());
return;
}
Init = Result.getAs<Expr>();

we reset Init it does not looks like we check anywhere in InitSeq.Perform(...) for containsErrors.

We can add a check here if (Result.isInvalid()) to create a recovery expression but not clear if that is the right place to do it but it looks like that does an ok job for this case.

CC @hokein @AaronBallman

@hokein hokein closed this as completed in 9ca395b Jul 11, 2023
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jul 19, 2023
  [clang][AST] Propagate the contains-errors bit to DeclRefExpr from VarDecl's initializer.

  Similar to the https://reviews.llvm.org/D86048 (it only sets the bit for C++
  code), we propagate the contains-errors bit for C-code path.

  Fixes llvm/llvm-project#50236
  Fixes llvm/llvm-project#50243
  Fixes llvm/llvm-project#48636
  Fixes llvm/llvm-project#50320

  Differential Revision: https://reviews.llvm.org/D154861

This fixes an assertion ('Assertion failed: ((LHSExpr->containsErrors()
|| RHSExpr->containsErrors()) && "Should only occur in error-recovery
path."), function BuildBinOp') when building parts of dtrace in certain
scenarios.

Reported by:	dstolfa
PR:		271047
MFC after:	1 month
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jul 23, 2023
  [clang][AST] Propagate the contains-errors bit to DeclRefExpr from VarDecl's initializer.

  Similar to the https://reviews.llvm.org/D86048 (it only sets the bit for C++
  code), we propagate the contains-errors bit for C-code path.

  Fixes llvm/llvm-project#50236
  Fixes llvm/llvm-project#50243
  Fixes llvm/llvm-project#48636
  Fixes llvm/llvm-project#50320

  Differential Revision: https://reviews.llvm.org/D154861

This fixes an assertion ('Assertion failed: ((LHSExpr->containsErrors()
|| RHSExpr->containsErrors()) && "Should only occur in error-recovery
path."), function BuildBinOp') when building parts of dtrace in certain
scenarios.

Reported by:	dstolfa
PR:		271047
MFC after:	1 month

(cherry picked from commit 8d0cab8)
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this issue Sep 6, 2023
  [clang][AST] Propagate the contains-errors bit to DeclRefExpr from VarDecl's initializer.

  Similar to the https://reviews.llvm.org/D86048 (it only sets the bit for C++
  code), we propagate the contains-errors bit for C-code path.

  Fixes llvm/llvm-project#50236
  Fixes llvm/llvm-project#50243
  Fixes llvm/llvm-project#48636
  Fixes llvm/llvm-project#50320

  Differential Revision: https://reviews.llvm.org/D154861

This fixes an assertion ('Assertion failed: ((LHSExpr->containsErrors()
|| RHSExpr->containsErrors()) && "Should only occur in error-recovery
path."), function BuildBinOp') when building parts of dtrace in certain
scenarios.

Reported by:	dstolfa
PR:		271047
MFC after:	1 month
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party crash-on-invalid
Projects
None yet
Development

No branches or pull requests

5 participants