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

[clang][Sema] `++`/`--` on `_Atomic` function pointer crashes in diagnoseArithmeticOnFunctionPointer (release builds too)

Open skaeads12 opened this issue 2 months ago • 2 comments

++/-- on an _Atomic function pointer asserts in diagnoseArithmeticOnFunctionPointer, because the _Atomic wrapper is not stripped before the pointee type is read. Release builds segfault as soon as the diagnostic is printed (-Wpointer-arith/-pedantic in C, default flags in C++).

Reproducer

https://godbolt.org/z/sbb76zoMx (C: assertions trunk -c, release trunk -c -Wpointer-arith, GCC 16.2 -c -pedantic; C++: release trunk -c)

void f(void) { _Atomic(int (*)(void)) b; b++; }

clang --target=x86_64-unknown-linux-gnu -c repro.c (release builds: add -Wpointer-arith or -pedantic)

Assertions build:

clang: clang/lib/Sema/SemaExpr.cpp:11564: void diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*): Assertion `Pointer->getType()->isAnyPointerType()' failed.
 #10 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
 #11 checkArithmeticOpPointerOperand(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
 #12 CheckIncrementDecrementOperand(...) SemaExpr.cpp
 #13 clang::Sema::CreateBuiltinUnaryOp(clang::SourceLocation, clang::UnaryOperatorKind, clang::Expr*, bool)

Release build with -Wpointer-arith:

 #4 ConvertTypeToDiagnosticString(clang::ASTContext&, clang::QualType, ...) ASTDiagnostic.cpp
 #5 clang::FormatASTNodeDiagnosticArgument(...)
 #6 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl<char>&) const
#13 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
Program terminated with signal SIGSEGV (11)

Root cause

  1. checkArithmeticOpPointerOperand looks through the AtomicType (ResAtomicType->getValueType()) to find the function pointee, but then passes the original _Atomic operand to diagnoseArithmeticOnFunctionPointer.
  2. That function asserts Pointer->getType()->isAnyPointerType() and passes Pointer->getType()->getPointeeType() to the diagnostic. For an AtomicType that is a null QualType.
  3. In release builds the assertion is compiled out. With default C flags the GNU-extension warning is off, so the null type is never formatted and the build exits 0. With -Wpointer-arith or -pedantic the diagnostic printer segfaults. In C++ the same diagnostic is an error, so release clang++ segfaults with default flags.

Related spellings behave differently: --b is the same; b + 1 compiles (its operand has already gone through lvalue conversion, which drops _Atomic); b += 1 is rejected with invalid operands to binary expression ('_Atomic(int (*)(void))' and 'int'). GCC accepts all three. _Atomic(void *) v; v++; does not crash, because diagnoseArithmeticOnVoidPointer does not use the type.

Suggested fix (untested)

Strip _Atomic in diagnoseArithmeticOnFunctionPointer too, e.g. use Pointer->getType().getAtomicUnqualifiedType() for both the assertion and the pointee type.

Details

  • Classification: in C, crash on valid GNU C (arithmetic on function pointers is a GNU extension; GCC 16.2 accepts b++, and warns only with -pedantic). In C++, crash on invalid code (int (*p)(); p++; is an error there), and the assertion fires before that error is emitted.
  • History: long-standing. The same assertion fires in assertions builds from 3.7 to trunk; 3.6 compiles the file with no diagnostic. Release builds tried from 3.7.1 to 23.1 (3.7.1, 9.0, 16.0, 22.1, 23.1) segfault with -Wpointer-arith; 3.6 does not. Likely cause: cf7d164ec1c8 ("Sema: Semantically check _Atomic-qualified pointers", 2015-02-12), which added the AtomicType unwrapping to checkArithmeticOpPointerOperand but not to diagnoseArithmeticOnFunctionPointer. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a as described above.
  • Related: none found.
  • Found by a fuzzer (generated C program), reduced by hand.

skaeads12 avatar Oct 01 '26 10:10 skaeads12

@llvm/issue-subscribers-clang-frontend

Author: skaeads12

`++`/`--` on an `_Atomic` function pointer asserts in `diagnoseArithmeticOnFunctionPointer`, because the `_Atomic` wrapper is not stripped before the pointee type is read. Release builds segfault as soon as the diagnostic is printed (`-Wpointer-arith`/`-pedantic` in C, default flags in C++).

Reproducer

https://godbolt.org/z/sbb76zoMx (C: assertions trunk -c, release trunk -c -Wpointer-arith, GCC 16.2 -c -pedantic; C++: release trunk -c)

void f(void) { _Atomic(int (*)(void)) b; b++; }

clang --target=x86_64-unknown-linux-gnu -c repro.c (release builds: add -Wpointer-arith or -pedantic)

Assertions build:

clang: clang/lib/Sema/SemaExpr.cpp:11564: void diagnoseArithmeticOnFunctionPointer(clang::Sema&amp;, clang::SourceLocation, clang::Expr*): Assertion `Pointer-&gt;getType()-&gt;isAnyPointerType()' failed.
 #<!-- -->10 diagnoseArithmeticOnFunctionPointer(clang::Sema&amp;, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
 #<!-- -->11 checkArithmeticOpPointerOperand(clang::Sema&amp;, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
 #<!-- -->12 CheckIncrementDecrementOperand(...) SemaExpr.cpp
 #<!-- -->13 clang::Sema::CreateBuiltinUnaryOp(clang::SourceLocation, clang::UnaryOperatorKind, clang::Expr*, bool)

Release build with -Wpointer-arith:

 #<!-- -->4 ConvertTypeToDiagnosticString(clang::ASTContext&amp;, clang::QualType, ...) ASTDiagnostic.cpp
 #<!-- -->5 clang::FormatASTNodeDiagnosticArgument(...)
 #<!-- -->6 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl&lt;char&gt;&amp;) const
#<!-- -->13 diagnoseArithmeticOnFunctionPointer(clang::Sema&amp;, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
Program terminated with signal SIGSEGV (11)

Root cause

  1. checkArithmeticOpPointerOperand looks through the AtomicType (ResAtomicType-&gt;getValueType()) to find the function pointee, but then passes the original _Atomic operand to diagnoseArithmeticOnFunctionPointer.
  2. That function asserts Pointer-&gt;getType()-&gt;isAnyPointerType() and passes Pointer-&gt;getType()-&gt;getPointeeType() to the diagnostic. For an AtomicType that is a null QualType.
  3. In release builds the assertion is compiled out. With default C flags the GNU-extension warning is off, so the null type is never formatted and the build exits 0. With -Wpointer-arith or -pedantic the diagnostic printer segfaults. In C++ the same diagnostic is an error, so release clang++ segfaults with default flags.

Related spellings behave differently: --b is the same; b + 1 compiles (its operand has already gone through lvalue conversion, which drops _Atomic); b += 1 is rejected with invalid operands to binary expression ('_Atomic(int (*)(void))' and 'int'). GCC accepts all three. _Atomic(void *) v; v++; does not crash, because diagnoseArithmeticOnVoidPointer does not use the type.

Suggested fix (untested)

Strip _Atomic in diagnoseArithmeticOnFunctionPointer too, e.g. use Pointer-&gt;getType().getAtomicUnqualifiedType() for both the assertion and the pointee type.

Details

  • Classification: in C, crash on valid GNU C (arithmetic on function pointers is a GNU extension; GCC 16.2 accepts b++, and warns only with -pedantic). In C++, crash on invalid code (int (*p)(); p++; is an error there), and the assertion fires before that error is emitted.
  • History: long-standing. The same assertion fires in assertions builds from 3.7 to trunk; 3.6 compiles the file with no diagnostic. Release builds tried from 3.7.1 to 23.1 (3.7.1, 9.0, 16.0, 22.1, 23.1) segfault with -Wpointer-arith; 3.6 does not. Likely cause: cf7d164ec1c8 ("Sema: Semantically check _Atomic-qualified pointers", 2015-02-12), which added the AtomicType unwrapping to checkArithmeticOpPointerOperand but not to diagnoseArithmeticOnFunctionPointer. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a as described above.
  • Related: none found.
  • Found by a fuzzer (generated C program), reduced by hand.

working on this

dineshtavva23 avatar Oct 18 '26 08:10 dineshtavva23