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

[clang] Constant evaluator asserts "No array filler" for an initialized array of >= 2^32 elements (unsigned APValue size)

Open skaeads12 opened this issue 2 months ago • 4 comments

The constant evaluator asserts "No array filler" when it reads an element of an initialized array with 2^32 or more elements, because APValue stores array sizes as unsigned. Valid C; assertions builds only.

Reproducer

https://godbolt.org/z/EfEa1c8bM

const char a[4294967296] = {0};
int b(void) { return a[5]; }

clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c (-O0 -S and -O2 -c assert too)

clang: clang/include/clang/AST/APValue.h:642: clang::APValue& clang::APValue::getArrayFiller(): Assertion `hasArrayFiller() && "No array filler"' failed.
 #11 findSubobject<(anonymous namespace)::ExtractSubobjectHandler>(...) ExprConstant.cpp
 #12 handleLValueToRValueConversion(...) ExprConstant.cpp
 #15 TryGetExprRange(...) SemaChecking.cpp
 #16 clang::Sema::CheckImplicitConversion(...)

Root cause

  1. ArrayExprEvaluator::VisitCXXParenListOrInitListExpr does unsigned NumElts = CAT->getZExtSize();. For 2^32 elements this is 0, so the initializer is evaluated into an array APValue with 0 elements and no filler.
  2. findSubobject range-checks a[5] against the real 64-bit size, finds no initialized element, and calls getArrayFiller(), which asserts. Going by the source, a release build reads Elts[0] of a zero-length allocation instead.
  3. The evaluator runs here because of the implicit-conversion range check on return a[5] (TryGetExprRange), so no -O is needed.

Every size from 2^32 up can hit this: 2^32+1 and 2^33 also assert, while 2^32-1 compiles. A string-literal initializer (= "x") asserts the same way, because expandStringLiteral has the same unsigned Elts = CAT->getZExtSize().

The evaluator already has a guard for this. EvalInfo::CheckArraySize ("APValue stores array extents as unsigned ...") makes evaluation fail for arrays of more than UINT_MAX elements. It was added for #63562 (45ab2b48bd55) and is used for new[], destruction and array expansion on writes, but not when an array value is built from an initializer list or a string literal. #138673 once added a CheckArraySize call to VisitCXXParenListOrInitListExpr, but it was reverted the same day (cc281584a11a), and it passed the already truncated count, so it would not have caught this case.

Suggested fix (untested)

Call CheckArraySize with the untruncated size in VisitCXXParenListOrInitListExpr and expandStringLiteral before building the array value.

Details

  • Classification: crash on valid code. {0} is plain C17; GCC 16.2 with -std=c17 -pedantic -Wall -Wextra compiles it without diagnostics. Release builds compile it without diagnostics.
  • History: present since clang 17; 14.0 and 16.0 assertions builds compile it. 17.0 and 18.1 fail earlier with isa<> used on a null pointer for = {0}, and hit this assertion for = {}. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29).
  • Related: #63562 (its fix added the guard above). #54766 (open) and #57353 (closed) are the same class of 32-bit size truncation, but in CodeGen. CodeGen emits global initializers from the AST (= {1} on this array is emitted correctly), so this is likely a separate bug.
  • Found by a fuzzer (generated C program), reduced by hand.

skaeads12 avatar Sep 30 '26 01:09 skaeads12

@llvm/issue-subscribers-clang-frontend

Author: skaeads12

https://godbolt.org/z/EfEa1c8bM (assertions trunk; also release trunk and GCC 16.2)
const char a[4294967296] = {0};
int b(void) { return a[5]; }
clang: /root/llvm-project/llvm/tools/clang/include/clang/AST/APValue.h:642: clang::APValue&amp; clang::APValue::getArrayFiller(): Assertion `hasArrayFiller() &amp;&amp; "No array filler"' failed.
 #<!-- -->11 findSubobject&lt;(anonymous namespace)::ExtractSubobjectHandler&gt;(...) ExprConstant.cpp
 #<!-- -->12 handleLValueToRValueConversion(...) ExprConstant.cpp
 #<!-- -->14 clang::Expr::EvaluateAsRValue(...)
 #<!-- -->15 TryGetExprRange(...) SemaChecking.cpp
 #<!-- -->16 clang::Sema::CheckImplicitConversion(...)

APValue stores array sizes as unsigned. ArrayExprEvaluator::VisitCXXParenListOrInitListExpr does unsigned NumElts = CAT-&gt;getZExtSize();, so 2^32 elements become 0, and the initializer is evaluated into an array APValue with 0 elements and no filler. findSubobject range-checks a[5] against the real 64-bit size and finds no initialized element, so it calls getArrayFiller(), which asserts. Going by the source, a release build reads Elts[0] of a zero-length allocation instead. Here the evaluator is run by the implicit-conversion range check on return a[5] (TryGetExprRange), so no -O is needed. Sizes of 2^32 and above can hit it: 2^32+1 and 2^33 also assert, while 2^32-1 compiles. A string-literal initializer (= "x") asserts the same way, since expandStringLiteral has the same unsigned Elts = CAT-&gt;getZExtSize().

The evaluator already has a guard for this. EvalInfo::CheckArraySize ("APValue stores array extents as unsigned ...") makes evaluation fail for arrays of more than UINT_MAX elements. It was added for #63562 (45ab2b48bd55) and is used for new[], destruction and array expansion on writes, but not when an array value is built from an initializer list or a string literal. #138673 once added a CheckArraySize call to VisitCXXParenListOrInitListExpr, but it was reverted the same day (cc281584a11a) because the constexpr-steps limit broke a build. It also passed the already truncated NumEltsToInit, so it would not have caught this case.

Found by a fuzzer (generated C program), reduced by hand. Command: clang --target=x86_64-unknown-linux-gnu -std=c17 -pedantic -fsyntax-only repro.c (-O0 -S and -O2 -c assert too; -fsyntax-only only avoids writing a 4 GiB object when the compiler does not crash) Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): assertion above, also with = {} and default flags. Release trunk f4085066128a (2026-09-29): compiles without diagnostics. History: not bisected. Assertion builds 14.0.0 and 16.0.0 compile it. 17.0.1 and 18.1.0 crash too: with = {} they hit this assertion, and with = {0} they fail isa&lt;&gt; used on a null pointer earlier, in VisitCXXParenListOrInitListExpr (the 17.0.1 and 18.1.0 release builds segfault there). 19.1.0, 22.1.0, 23.1.0 and trunk hit this assertion for both forms. Release builds 16.0.0, 19.1.0 and trunk compile it. So it has been present since clang 17. Classification: crash on valid code. {0} is plain C17, and GCC 16.2 with -std=c17 -pedantic -Wall -Wextra compiles it without diagnostics. Related: #63562 (closed), whose fix added the guard above. #54766 (open) and #57353 (closed) are the same class of 32-bit size truncation for huge arrays, but in CodeGen. CodeGen emits global initializers from the AST before falling back to the APValue (= {1} on this array is emitted correctly), so this is likely a separate bug, not the same root cause as #54766. I found no report of this assertion (searched "No array filler", hasArrayFiller, getArrayFiller).

The summary reads like a stream of consciousness dump, this needs some trimming and reformatting to make it useful for a triager.

shafik avatar Sep 30 '26 02:09 shafik

The summary reads like a stream of consciousness dump, this needs some trimming and reformatting to make it useful for a triager.

Thanks, I've reconstructed the description and trim the redundant parts.

skaeads12 avatar Sep 30 '26 02:09 skaeads12

Is this fixed by #226899 ?

efriedma-quic avatar Sep 30 '26 04:09 efriedma-quic