llvm / llvm/llvm-project

[clang] Assertion `Result && "Could not evaluate expression"' failed with __builtin_object_size in enable_if

Open
#216,043 5 comments 0 reactions 0 assignees View on GitHub
clang:frontend crash generated by fuzzer
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproducer:
https://godbolt.org/z/ET4GMGbrb

With an assertion-enabled Clang trunk build in C++ mode:

```c
int fun(int x) { int y = x; return y; }

int strnlen(char *s, int maxlen)
__attribute__( (
enable_if( __builtin_object_size(s, fun(0) ) ,"!") ) )
{
return 0;
}
```

Backtrace:

```
clang++: /root/llvm-project/llvm/tools/clang/lib/AST/ExprConstant.cpp:22192: llvm::APSInt clang::Expr::EvaluateKnownConstInt(const clang::ASTContext&) const: Assertion `Result && "Could not evaluate expression"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics
1. :6:1: current parser token '{'
#0 0x0000000004468688 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4468688)
#1 0x0000000004465534 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4465534)
#2 0x0000000004465b6c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4465b6c)
#3 0x00000000043a4448 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x0000788cb4445330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x0000788cb449ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
#6 0x0000788cb444527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x0000788cb44288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x0000788cb442881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x0000788cb443b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x00000000081727e9 clang::Expr::EvaluateKnownConstInt(clang::ASTContext const&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x81727e9)
#11 0x00000000081d074a (anonymous namespace)::IntExprEvaluator::VisitBuiltinCallExpr(clang::CallExpr const*, unsigned int) ExprConstant.cpp:0:0
#12 0x00000000081d670b (anonymous namespace)::IntExprEvaluator::VisitCallExpr(clang::CallExpr const*) ExprConstant.cpp:0:0
#13 0x00000000081973d8 clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#14 0x000000000816a0ff Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
#15 0x0000000008174548 EvaluateAsBooleanCondition(clang::Expr const*, bool&, (anonymous namespace)::EvalInfo&) ExprConstant.cpp:0:0
#16 0x00000000081c910b (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#17 0x00000000081984bc clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#18 0x000000000816a0ff Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
#19 0x000000000817c0ee clang::Expr::isPotentialConstantExprUnevaluated(clang::Expr*, clang::FunctionDecl const*, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x817c0ee)
#20 0x00000000071d1cd0 checkFunctionConditionAttr(clang::Sema&, clang::Decl*, clang::ParsedAttr const&, clang::Expr*&, llvm::StringRef&) (.part.0) SemaDeclAttr.cpp:0:0
#21 0x00000000071eb5c3 handleEnableIfAttr(clang::Sema&, clang::Decl*, clang::ParsedAttr const&) SemaDeclAttr.cpp:0:0
#22 0x0000000007209c20 ProcessDeclAttribute(clang::Sema&, clang::Scope*, clang::Decl*, clang::ParsedAttr const&, clang::Sema::ProcessDeclAttributeOptions const&) (.isra.0) SemaDeclAttr.cpp:0:0
#23 0x000000000720c1db clang::Sema::ProcessDeclAttributes(clang::Scope*, clang::Decl*, clang::Declarator const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x720c1db)
#24 0x00000000071b9a4b clang::Sema::ActOnFunctionDeclarator(clang::Scope*, clang::Declarator&, clang::DeclContext*, clang::TypeSourceInfo*, clang::LookupResult&, llvm::MutableArrayRef, bool&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71b9a4b)
#25 0x00000000071c05ab clang::Sema::HandleDeclarator(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71c05ab)
#26 0x00000000071c1238 clang::Sema::ActOnStartOfFunctionDef(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef, clang::SkipBodyInfo*, clang::Sema::FnBodyKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71c1238)
#27 0x0000000006d99a0f clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d99a0f)
#28 0x0000000006de3c86 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6de3c86)
#29 0x0000000006d8e64b clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d8e64b)
#30 0x0000000006d8ee3f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d8ee3f)
#31 0x0000000006d9b5f8 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d9b5f8)
#32 0x0000000006d9c555 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d9c555)
#33 0x0000000006d794aa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d794aa)
#34 0x0000000004f62718 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f62718)
#35 0x000000000528edf8 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x528edf8)
#36 0x000000000521bf8d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x521bf8d)
#37 0x0000000005364abd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5364abd)
#38 0x0000000000dabaac cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdabaac)
#39 0x0000000000da26ca ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#40 0x0000000000da284d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#41 0x0000000004fdb1f9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#42 0x00000000043a4884 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43a4884)
#43 0x0000000004fdbd65 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#44 0x0000000004f93c12 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f93c12)
#45 0x0000000004f98577 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f98577)
#46 0x0000000004fa2ae5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4fa2ae5)
#47 0x0000000000da7faa clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda7faa)
#48 0x0000000000cc3c5a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcc3c5a)
#49 0x0000788cb442a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#50 0x0000788cb442a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#51 0x0000000000da2075 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda2075)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```

This reproduces all the way back to Clang 9.0.0
This testcase was generated by fuzzer.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the compiler assertion with the Godbolt testcase in C++ mode using an assertion-enabled Clang build. Read llvm-project/clang/lib/AST/ExprConstant.cpp and the enable_if handling in SemaDeclAttr.cpp, following the stack from EvaluateKnownConstInt; done means the testcase no longer aborts with the reported assertion and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.