llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][Sema] `__swift_attr__` with no argument in type position crashes in HandleSwiftAttr (regression since clang 20)

Open skaeads12 opened this issue 2 months ago • 2 comments

__swift_attr__ without an argument in type-attribute position reads argument 0 of an empty argument list. Assertions builds assert, and release builds segfault instead of reporting the error. Invalid code; regression in clang 20.

Reproducer

https://godbolt.org/z/zYnT89fbh (assertions trunk, release trunk, 19.1.0, 20.1.0)

int * __attribute__((__swift_attr__)) a;

clang --target=x86_64-unknown-linux-gnu -c repro.c (same with clang++)

clang: clang/include/clang/Sema/ParsedAttr.h:375: clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.
 #10 clang::ParsedAttr::getArgAsExpr(unsigned int) const
 #11 clang::Sema::checkStringLiteralArgumentAttr(clang::ParsedAttr const&, unsigned int, llvm::StringRef&, clang::SourceLocation*)
 #12 processTypeAttrs(...) SemaType.cpp
 #13 GetFullTypeForDeclarator(...) SemaType.cpp

Root cause

  • In declaration position the same mistake is diagnosed: __attribute__((__swift_attr__)) int a; gives error: '__swift_attr__' attribute takes one argument. There, ProcessDeclAttribute runs checkCommonAttributeFeatures, which includes the argument-count check, before SemaSwift::handleAttrAttr.
  • On a declarator chunk the attribute goes to HandleSwiftAttr (SemaType.cpp, inlined into processTypeAttrs above) instead. That function calls S.checkStringLiteralArgumentAttr(PAttr, 0, Str) without checking the argument count, and getArgAsExpr(0) reads past the end of the empty argument list.
  • Type-attribute handlers have to do that check themselves; HandleBTFTypeTagAttribute, for example, checks Attr.getNumArgs() != 1.

Suggested fix (untested)

if (!PAttr.checkExactlyNumArgs(S, 1)) { PAttr.setInvalid(); return; } before the checkStringLiteralArgumentAttr call in HandleSwiftAttr.

Details

  • Classification: crash on invalid code. __swift_attr__ requires one string argument, and the declaration-position form and clang 16.0 to 19.1 reject this input with the error above.
  • Release builds: segfault in the same place (checkStringLiteralArgumentAttr -> Expr::IgnoreParenCasts), C and C++.
  • History: regression in clang 20. 16.0, 18.1 and 19.1 (release) and the 19.1 assertions build emit the error; 20.1 segfaults (release) or asserts (assertions build). Likely cause: #108631 ("[clang/AST] Make it possible to use SwiftAttr in type context", d3daa3c4435a), which added HandleSwiftAttr with this unchecked call. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a segfaults.
  • Related: none found.
  • Found by a fuzzer (generated C program), reduced by hand.

skaeads12 avatar Sep 30 '26 03:09 skaeads12

@llvm/issue-subscribers-clang-frontend

Author: skaeads12

`__swift_attr__` without an argument in type-attribute position reads argument 0 of an empty argument list. Assertions builds assert, and release builds segfault instead of reporting the error. Invalid code; regression in clang 20.

Reproducer

https://godbolt.org/z/zYnT89fbh (assertions trunk, release trunk, 19.1.0, 20.1.0)

int * __attribute__((__swift_attr__)) a;

clang --target=x86_64-unknown-linux-gnu -c repro.c (same with clang++)

clang: clang/include/clang/Sema/ParsedAttr.h:375: clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg &lt; NumArgs &amp;&amp; "Arg access out of range!"' failed.
 #<!-- -->10 clang::ParsedAttr::getArgAsExpr(unsigned int) const
 #<!-- -->11 clang::Sema::checkStringLiteralArgumentAttr(clang::ParsedAttr const&amp;, unsigned int, llvm::StringRef&amp;, clang::SourceLocation*)
 #<!-- -->12 processTypeAttrs(...) SemaType.cpp
 #<!-- -->13 GetFullTypeForDeclarator(...) SemaType.cpp

Root cause

  • In declaration position the same mistake is diagnosed: __attribute__((__swift_attr__)) int a; gives error: '__swift_attr__' attribute takes one argument. There, ProcessDeclAttribute runs checkCommonAttributeFeatures, which includes the argument-count check, before SemaSwift::handleAttrAttr.
  • On a declarator chunk the attribute goes to HandleSwiftAttr (SemaType.cpp, inlined into processTypeAttrs above) instead. That function calls S.checkStringLiteralArgumentAttr(PAttr, 0, Str) without checking the argument count, and getArgAsExpr(0) reads past the end of the empty argument list.
  • Type-attribute handlers have to do that check themselves; HandleBTFTypeTagAttribute, for example, checks Attr.getNumArgs() != 1.

Suggested fix (untested)

if (!PAttr.checkExactlyNumArgs(S, 1)) { PAttr.setInvalid(); return; } before the checkStringLiteralArgumentAttr call in HandleSwiftAttr.

Details

  • Classification: crash on invalid code. __swift_attr__ requires one string argument, and the declaration-position form and clang 16.0 to 19.1 reject this input with the error above.
  • Release builds: segfault in the same place (checkStringLiteralArgumentAttr -> Expr::IgnoreParenCasts), C and C++.
  • History: regression in clang 20. 16.0, 18.1 and 19.1 (release) and the 19.1 assertions build emit the error; 20.1 segfaults (release) or asserts (assertions build). Likely cause: #108631 ("[clang/AST] Make it possible to use SwiftAttr in type context", d3daa3c4435a), which added HandleSwiftAttr with this unchecked call. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a segfaults.
  • Related: none found.
  • Found by a fuzzer (generated C program), reduced by hand.

@rjmccall @Bigcheese

shafik avatar Sep 30 '26 17:09 shafik