[Clang] Fix crash in __builtin_assume_aligned (#114217)

The CodeGen for __builtin_assume_aligned assumes that the first argument
is a pointer, so crashes if the int-conversion error is downgraded or
disabled. Emit a non-downgradable error if the argument is not a
pointer, like we currently do for __builtin_launder.

Fixes #110914.
This commit is contained in:
Oliver Stannard 2024-12-19 10:34:56 +00:00 committed by GitHub
parent e020f46027
commit ecdc5289af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 5 deletions

View File

@ -51,8 +51,8 @@ void my_printf(const char* format, ...) {
int my_vprintf(const char* format, va_list arg ); // OK to declare function taking va_list
void ignoredBuiltinsTest() {
(void)__builtin_assume_aligned(0, 8);
void ignoredBuiltinsTest(void *ptr) {
(void)__builtin_assume_aligned(ptr, 8);
(void)__builtin_constant_p(0);
(void)__builtin_fpclassify(0, 0, 0, 0, 0, 0.f);
(void)__builtin_isinf_sign(0.f);

View File

@ -12324,6 +12324,8 @@ def warn_noderef_to_dereferenceable_pointer : Warning<
def err_builtin_launder_invalid_arg : Error<
"%select{non-pointer|function pointer|void pointer}0 argument to "
"'__builtin_launder' is not allowed">;
def err_builtin_assume_aligned_invalid_arg : Error<
"non-pointer argument to '__builtin_assume_aligned' is not allowed">;
def err_builtin_is_within_lifetime_invalid_arg : Error<
"%select{non-|function }0pointer argument to '__builtin_is_within_lifetime' "

View File

@ -5341,9 +5341,11 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
{
ExprResult FirstArgResult =
DefaultFunctionArrayLvalueConversion(FirstArg);
if (checkBuiltinArgument(*this, TheCall, 0))
if (!FirstArgResult.get()->getType()->isPointerType()) {
Diag(TheCall->getBeginLoc(), diag::err_builtin_assume_aligned_invalid_arg)
<< TheCall->getSourceRange();
return true;
/// In-place updation of FirstArg by checkBuiltinArgument is ignored.
}
TheCall->setArg(0, FirstArgResult.get());
}

View File

@ -0,0 +1,9 @@
// RUN: %clang_cc1 -fsyntax-only -Wno-int-conversion -triple x86_64-linux -verify %s
// Check that the pointer->int conversion error is not downgradable for the
// pointer argument to __builtin_assume_aligned.
int test(int *a, int b) {
a = (int *)__builtin_assume_aligned(b, 32); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
int *y = __builtin_assume_aligned(1, 1); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
}

View File

@ -74,7 +74,7 @@ int test13(int *a) {
}
int test14(int *a, int b) {
a = (int *)__builtin_assume_aligned(b, 32); // expected-error {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const void *}}
a = (int *)__builtin_assume_aligned(b, 32); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
}
int test15(int *b) {