Issue 86998
Summary [clang][CoverageMapping] assertion failure during compiling `switch` within statement expressions
Labels clang
Assignees
Reporter 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.,

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

Compiler explorer [link](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:2,endLineNumber:33,positionColumn:2,positionLineNumber:33,selectionStartColumn:2,selectionStartLineNumber:33,startColumn:2,startLineNumber:33),source:'//+An+example+simplified+from+https://elixir.bootlin.com/linux/v6.8.1/source/fs/coredump.c.%0A//%0A//+Note+that+assertions+should+be+enabled,+therefore+prebuilt+packages+from%0A//+apt.llvm.org+etc+might+not+behave+the+same.%0A//%0A//+Assertion+failure:%0A//%0A//+++$+clang+-fprofile-instr-generate+-fcoverage-mapping+this_file.c%0A//%0A//+No+assertion+failure:%0A//%0A//+++$+clang+this_file.c%0A//%0A%0Astruct+Foo+%7B%0A++++int+field1%3B%0A++++int+field2%3B+//+.field2+must+be+defined+but+not+be+intialized%0A%7D%3B%0A%0Aint+main(int+argc,+char+**argv)+%7B%0A++++//+https://elixir.bootlin.com/linux/v6.8.1/source/fs/coredump.c%23L545%0A++++struct+Foo+foo+%3D+%7B%0A++++++++.field1+%3D+(%7B%0A++++++++++++switch+(123)+%7B%0A++++++++++++case+123:%0A++++++++++++++++break%3B%0A++++++++++++%7D%0A++++++++++++456%3B%0A++++++++%7D),%0A++++++++//+.field2+must+be+defined+but+not+be+intialized%0A++++%7D%3B%0A++++return+0%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:61.44745998608211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang_assertions_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'1',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:___c,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(assertions+trunk)+(Editor+%231)',t:'0')),k:34.5741843594503,l:'4',m:28.903654485049834,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(assertions+trunk)+(Compiler+%232)',t:'0')),header:(),l:'4',m:71.09634551495017,n:'0',o:'',s:0,t:'0')),k:38.55254001391788,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4)

```text
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
```




_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to