llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: nudt_yixiao (keepyixiao) <details> <summary>Changes</summary> 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. AI assistance was used to generate additional regression test cases, which I reviewed and adapted. Fixes https://github.com/llvm/llvm-project/issues/231108 --- Full diff: https://github.com/llvm/llvm-project/pull/231793.diff 3 Files Affected: - (modified) clang/docs/ReleaseNotes.md (+3) - (modified) clang/lib/AST/ExprConstant.cpp (+3-1) - (modified) clang/test/Sema/builtin-object-size.c (+16) ``````````diff diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index de4d8dfa7271e..e31190e6315dd 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 cf450699aee99..efa36e7ccc2da 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 a9d472402fc78..5c0ab4efd8724 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; `````````` </details> https://github.com/llvm/llvm-project/pull/231793 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
