llvm / llvm/llvm-project

[Clang] Assertion failure in SemaInit.cpp when parsing variable declaration with empty __asm__ label and parenthesized initializer

Open
#205,320 3 comments 0 reactions 0 assignees View on GitHub
c clang:frontend confirmed crash inline-asm
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Description:
Hi LLVM team,

I found another assertion failure in Clang (assertions trunk). When declaring a variable with an empty __asm__("") label followed by a parenthesized initializer, Clang correctly diagnoses the empty string literal but then crashes during the initialization sequence.

Thank you again for your hard work and dedication to improving Clang! Please let me know if you need any further information or testing on my end.

Steps to Reproduce:
Source Code (crash.c):

```c
char hello __asm__("")('c', 'c');
```

Compiler Version: x86-64 clang (assertions trunk)

Command Line: `clang -c crash.c`

(Note: Easily reproducible on Compiler Explorer using the assertions trunk).

Actual Behavior:
Clang correctly emits an error regarding the empty string literal used in the asm label. However, it fails to recover cleanly and immediately hits an assertion in SemaInit.cpp while attempting to process the initializer ('c', 'c').

```
:2:20: error: cannot use an empty string literal in 'asm'
2 | char hello __asm__("")('c', 'c');
| ^~

clang: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaInit.cpp:6917: void clang::InitializationSequence::InitializeFrom(clang::Sema&, ...): Assertion `...' 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 ...
1. :2:33: current parser token ';'

...
#10 0x000000000732c9f0 clang::InitializationSequence::InitializeFrom(clang::Sema&, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef, bool)
#11 0x000000000704967a clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool)
#12 0x0000000006c8d399 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*)
#13 0x0000000006c9a4da clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::SourceLocation*, clang::Parser::ForRangeInit*)
#14 0x0000000006c577ab clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier)
#15 0x0000000006c57faf clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier)
#16 0x0000000006c5fa61 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*)
#17 0x0000000006c60975 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&)
#18 0x0000000006c60d60 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&)
#19 0x0000000006c41823 clang::ParseAST(clang::Sema&, bool, bool)
...
```

Expected Behavior:
Clang should reject the invalid code by emitting the error for the empty asm label (and possibly errors regarding the invalid initializer format), but it should recover from the invalid AST state and terminate gracefully without causing an assertion failure.

Contributor guide

Open the contributing guide

Research direction

Start with tools/clang/lib/Sema/SemaInit.cpp, especially InitializationSequence::InitializeFrom, using the provided crash.c and `clang -c crash.c` command. Trace recovery after the empty asm-label diagnostic and add or update a regression test so the input reports diagnostics and exits without an assertion failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
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.