https://github.com/keepyixiao updated https://github.com/llvm/llvm-project/pull/231793
>From 4d81120e36623abf0b26dacb3355eb75d46df31f Mon Sep 17 00:00:00 2001 From: nudt_yixiao <[email protected]> Date: Wed, 14 Oct 2026 15:55:19 +0800 Subject: [PATCH 1/2] [Clang] Avoid assertions for unevaluable object size kinds Constant-evaluated calls to __builtin_object_size and __builtin_dynamic_object_size may have a second argument that cannot be evaluated. The legacy evaluator incorrectly assumed it was a known constant integer, which could trigger an assertion. Evaluate the argument through the current evaluator and propagate evaluation failures instead of calling EvaluateKnownConstInt. Add regression coverage for division by zero, function calls, runtime parameters, volatile reads, and null pointer dereferences. The tests cover both object-size builtins and both constant evaluators. --- clang/docs/ReleaseNotes.md | 3 +++ clang/lib/AST/ExprConstant.cpp | 4 +++- clang/test/Sema/builtin-object-size.c | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index de4d8dfa7271e6..e31190e6315dd2 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -729,6 +729,9 @@ features cannot lower the translation-unit ABI level; - Fixed an assertion failure when `__builtin_object_size` or `__builtin_dynamic_object_size` is called with an out-of-range type argument during constant evaluation. (#GH225041) +- Fixed an assertion failure when constant-evaluating `__builtin_object_size` + or `__builtin_dynamic_object_size` with a type argument that is not an + integer constant expression. (#GH231108) #### Bug Fixes to Attribute Support diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index cf450699aee999..efa36e7ccc2da3 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -17176,7 +17176,9 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, case Builtin::BI__builtin_dynamic_object_size: case Builtin::BI__builtin_object_size: { - APSInt TypeValue = E->getArg(1)->EvaluateKnownConstInt(Info.Ctx); + APSInt TypeValue; + if (!EvaluateInteger(E->getArg(1), TypeValue, Info)) + return false; if (TypeValue < 0 || TypeValue > 3) { Info.FFDiag(E->getArg(1), diag::note_constexpr_builtin_arg_out_of_range) << TypeValue << 0 << 3; diff --git a/clang/test/Sema/builtin-object-size.c b/clang/test/Sema/builtin-object-size.c index a9d472402fc780..5c0ab4efd87244 100644 --- a/clang/test/Sema/builtin-object-size.c +++ b/clang/test/Sema/builtin-object-size.c @@ -35,6 +35,22 @@ _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, -1), ""); // expected-error {{stat _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, 4), ""); // expected-error {{static assertion expression is not an integral constant expression}} \ expected-note {{argument value 4 is outside the valid range [0, 3]}} +_Static_assert(OBJECT_SIZE_BUILTIN((void *)0, 1 / 0), ""); // expected-error {{static assertion expression is not an integral constant expression}} \ + expected-note {{division by zero}} + +int get_object_size_type(void); +_Static_assert(OBJECT_SIZE_BUILTIN((void *)0, get_object_size_type()), ""); // expected-error {{static assertion expression is not an integral constant expression}} + +void test_non_constant_object_size_type(int type) { + _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, type), ""); // expected-error {{static assertion expression is not an integral constant expression}} +} + +volatile int volatile_object_size_type = 0; +_Static_assert(OBJECT_SIZE_BUILTIN((void *)0, volatile_object_size_type), ""); // expected-error {{static assertion expression is not an integral constant expression}} + +_Static_assert(OBJECT_SIZE_BUILTIN((void *)0, *(int *)0), ""); // expected-error {{static assertion expression is not an integral constant expression}} \ + expected-note {{dereferencing a null pointer is not allowed in a constant expression}} + // cannot call vsnprintf with va_list on x86_64 void f4(const char *fmt, ...) { __builtin_va_list args; >From 6fd76acc4d06d5afffcd4632f5af2d38ef42f1a8 Mon Sep 17 00:00:00 2001 From: nudt_yixiao <[email protected]> Date: Wed, 14 Oct 2026 20:57:36 +0800 Subject: [PATCH 2/2] Apply suggestion from @tbaederr Co-authored-by: Timm Baeder <[email protected]> --- clang/test/Sema/builtin-object-size.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/Sema/builtin-object-size.c b/clang/test/Sema/builtin-object-size.c index 5c0ab4efd87244..e0b2777045e74f 100644 --- a/clang/test/Sema/builtin-object-size.c +++ b/clang/test/Sema/builtin-object-size.c @@ -36,7 +36,7 @@ _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, 4), ""); // expected-error {{stati expected-note {{argument value 4 is outside the valid range [0, 3]}} _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, 1 / 0), ""); // expected-error {{static assertion expression is not an integral constant expression}} \ - expected-note {{division by zero}} + expected-note {{division by zero}} int get_object_size_type(void); _Static_assert(OBJECT_SIZE_BUILTIN((void *)0, get_object_size_type()), ""); // expected-error {{static assertion expression is not an integral constant expression}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
