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][CoverageMapping] "Assertion AfterLoc.isValid() failed" during compiling switch within statement expressions #86998

Closed
whentojump opened this issue Mar 28, 2024 · 2 comments · Fixed by #89564
Labels
clang:codegen coverage crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@whentojump
Copy link
Member

When a statement expression contains a switch statement during a partial struct initialization, source-based code coverage will crash. E.g.,

    struct Foo foo = {
        .field1 = ({
            switch (123) {
            case 123:
                break;
            }
            456;
        }),
    };

Compiler explorer link

clang: /root/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp:1238: std::optional<clang::SourceRange> {anonymous}::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation): Assertion `AfterLoc.isValid()' failed.
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 -fprofile-instr-generate -fcoverage-mapping <source>
1.	<eof> parser at end of file
2.	<source>:20:5: LLVM IR generation of declaration 'main'
3.	<source>:20:5: Generating code for declaration 'main'
 #0 0x0000000003922c78 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3922c78)
 #1 0x000000000392095c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x392095c)
 #2 0x00000000038671f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f5cbfc42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f5cbfc969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f5cbfc42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f5cbfc287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f5cbfc2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f5cbfc39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000003d79e01 (anonymous namespace)::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation) CoverageMappingGen.cpp:0:0
#10 0x0000000003d8327b (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#11 0x0000000003d81176 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#12 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#13 0x0000000003d81de0 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#14 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#15 0x0000000003d81e9f clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#16 0x0000000003d88c7b clang::CodeGen::CoverageMappingGen::emitCounterMapping(clang::Decl const*, llvm::raw_ostream&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d88c7b)
#17 0x0000000003d324a8 clang::CodeGen::CodeGenPGO::emitCounterRegionMapping(clang::Decl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d324a8)
#18 0x0000000003d67ddb clang::CodeGen::CodeGenPGO::assignRegionCounters(clang::GlobalDecl, llvm::Function*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d67ddb)
#19 0x0000000003cc5339 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3cc5339)
#20 0x0000000003d24952 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d24952)
#21 0x0000000003d1f205 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d1f205)
#22 0x0000000003d201e0 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d201e0)
#23 0x0000000003d29d83 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#24 0x00000000041cee86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#25 0x00000000041bf7a8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41bf7a8)
#26 0x00000000061c8fe4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x61c8fe4)
#27 0x00000000041cc9c8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41cc9c8)
#28 0x000000000444c039 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x444c039)
#29 0x00000000043d572e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x43d572e)
#30 0x000000000452eece clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x452eece)
#31 0x0000000000c3d6dc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3d6dc)
#32 0x0000000000c3696a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#33 0x000000000420f949 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
#34 0x00000000038676a4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38676a4)
#35 0x000000000420ff3f 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
#36 0x00000000041d62d5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41d62d5)
#37 0x00000000041d6d3d 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+0x41d6d3d)
#38 0x00000000041deaa5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41deaa5)
#39 0x0000000000c3ab75 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3ab75)
#40 0x0000000000b1dd24 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xb1dd24)
#41 0x00007f5cbfc29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#42 0x00007f5cbfc29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#43 0x0000000000c3645e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3645e)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Mar 28, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 28, 2024

@llvm/issue-subscribers-clang-codegen

Author: Wentao Zhang (whentojump)

When a [statement expression](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html) contains a `switch` statement during a partial struct initialization, source-based code coverage will crash. E.g.,
    struct Foo foo = {
        .field1 = ({
            switch (123) {
            case 123:
                break;
            }
            456;
        }),
    };

Compiler explorer link

clang: /root/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp:1238: std::optional&lt;clang::SourceRange&gt; {anonymous}::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation): Assertion `AfterLoc.isValid()' failed.
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 -fprofile-instr-generate -fcoverage-mapping &lt;source&gt;
1.	&lt;eof&gt; parser at end of file
2.	&lt;source&gt;:20:5: LLVM IR generation of declaration 'main'
3.	&lt;source&gt;:20:5: Generating code for declaration 'main'
 #<!-- -->0 0x0000000003922c78 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3922c78)
 #<!-- -->1 0x000000000392095c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x392095c)
 #<!-- -->2 0x00000000038671f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #<!-- -->3 0x00007f5cbfc42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->4 0x00007f5cbfc969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #<!-- -->5 0x00007f5cbfc42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #<!-- -->6 0x00007f5cbfc287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #<!-- -->7 0x00007f5cbfc2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #<!-- -->8 0x00007f5cbfc39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #<!-- -->9 0x0000000003d79e01 (anonymous namespace)::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation) CoverageMappingGen.cpp:0:0
#<!-- -->10 0x0000000003d8327b (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->11 0x0000000003d81176 clang::StmtVisitorBase&lt;llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void&gt;::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->12 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->13 0x0000000003d81de0 clang::StmtVisitorBase&lt;llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void&gt;::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->14 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->15 0x0000000003d81e9f clang::StmtVisitorBase&lt;llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void&gt;::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#<!-- -->16 0x0000000003d88c7b clang::CodeGen::CoverageMappingGen::emitCounterMapping(clang::Decl const*, llvm::raw_ostream&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d88c7b)
#<!-- -->17 0x0000000003d324a8 clang::CodeGen::CodeGenPGO::emitCounterRegionMapping(clang::Decl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d324a8)
#<!-- -->18 0x0000000003d67ddb clang::CodeGen::CodeGenPGO::assignRegionCounters(clang::GlobalDecl, llvm::Function*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d67ddb)
#<!-- -->19 0x0000000003cc5339 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3cc5339)
#<!-- -->20 0x0000000003d24952 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d24952)
#<!-- -->21 0x0000000003d1f205 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d1f205)
#<!-- -->22 0x0000000003d201e0 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d201e0)
#<!-- -->23 0x0000000003d29d83 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#<!-- -->24 0x00000000041cee86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#<!-- -->25 0x00000000041bf7a8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41bf7a8)
#<!-- -->26 0x00000000061c8fe4 clang::ParseAST(clang::Sema&amp;, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x61c8fe4)
#<!-- -->27 0x00000000041cc9c8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41cc9c8)
#<!-- -->28 0x000000000444c039 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x444c039)
#<!-- -->29 0x00000000043d572e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x43d572e)
#<!-- -->30 0x000000000452eece clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x452eece)
#<!-- -->31 0x0000000000c3d6dc cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3d6dc)
#<!-- -->32 0x0000000000c3696a ExecuteCC1Tool(llvm::SmallVectorImpl&lt;char const*&gt;&amp;, llvm::ToolContext const&amp;) driver.cpp:0:0
#<!-- -->33 0x000000000420f949 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const::'lambda'()&gt;(long) Job.cpp:0:0
#<!-- -->34 0x00000000038676a4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref&lt;void ()&gt;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38676a4)
#<!-- -->35 0x000000000420ff3f clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const (.part.0) Job.cpp:0:0
#<!-- -->36 0x00000000041d62d5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&amp;, clang::driver::Command const*&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41d62d5)
#<!-- -->37 0x00000000041d6d3d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41d6d3d)
#<!-- -->38 0x00000000041deaa5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41deaa5)
#<!-- -->39 0x0000000000c3ab75 clang_main(int, char**, llvm::ToolContext const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3ab75)
#<!-- -->40 0x0000000000b1dd24 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xb1dd24)
#<!-- -->41 0x00007f5cbfc29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->42 0x00007f5cbfc29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->43 0x0000000000c3645e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3645e)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

@whentojump whentojump changed the title [clang][CoverageMapping] assertion failure during compiling switch within statement expressions [clang][CoverageMapping] "Assertion AfterLoc.isValid()' failed" during compiling switch` within statement expressions Mar 28, 2024
@whentojump whentojump changed the title [clang][CoverageMapping] "Assertion AfterLoc.isValid()' failed" during compiling switch` within statement expressions [clang][CoverageMapping] "Assertion AfterLoc.isValid() failed" during compiling switch within statement expressions Mar 28, 2024
@EugeneZelenko EugeneZelenko added crash Prefer [crash-on-valid] or [crash-on-invalid] and removed clang Clang issues not falling into any other category labels Mar 28, 2024
@whentojump
Copy link
Member Author

The root cause is somewhat clearer and here are two refined examples: 1, 2

The more accurate triggering condition is: In AST, any region terminator (like break and goto) is followed by a stmt with <invalid sloc> (like ImplicitValueInitExpr). Because a terminator is seen, the below branch will be executed when visiting the 2nd stmt:

if (LastStmt && HasTerminateStmt && !isa<AttributedStmt>(Child)) {
auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));

However, the 2nd stmt doesn't have a valid source location and will fail some assertions in findGapAreaBetween().

whentojump added a commit that referenced this issue Apr 22, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this issue Apr 28, 2024
…sn't have valid source locations (llvm#89564)

Fixes llvm#86998

(cherry picked from commit c1b6cca)
tstellar pushed a commit to llvmbot/llvm-project that referenced this issue Apr 30, 2024
…sn't have valid source locations (llvm#89564)

Fixes llvm#86998

(cherry picked from commit c1b6cca)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen coverage crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
3 participants