Skip to content

Commit ecdc528

Browse files
authored
[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.
1 parent e020f46 commit ecdc528

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void my_printf(const char* format, ...) {
5151

5252
int my_vprintf(const char* format, va_list arg ); // OK to declare function taking va_list
5353

54-
void ignoredBuiltinsTest() {
55-
(void)__builtin_assume_aligned(0, 8);
54+
void ignoredBuiltinsTest(void *ptr) {
55+
(void)__builtin_assume_aligned(ptr, 8);
5656
(void)__builtin_constant_p(0);
5757
(void)__builtin_fpclassify(0, 0, 0, 0, 0, 0.f);
5858
(void)__builtin_isinf_sign(0.f);

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12324,6 +12324,8 @@ def warn_noderef_to_dereferenceable_pointer : Warning<
1232412324
def err_builtin_launder_invalid_arg : Error<
1232512325
"%select{non-pointer|function pointer|void pointer}0 argument to "
1232612326
"'__builtin_launder' is not allowed">;
12327+
def err_builtin_assume_aligned_invalid_arg : Error<
12328+
"non-pointer argument to '__builtin_assume_aligned' is not allowed">;
1232712329

1232812330
def err_builtin_is_within_lifetime_invalid_arg : Error<
1232912331
"%select{non-|function }0pointer argument to '__builtin_is_within_lifetime' "

clang/lib/Sema/SemaChecking.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5341,9 +5341,11 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
53415341
{
53425342
ExprResult FirstArgResult =
53435343
DefaultFunctionArrayLvalueConversion(FirstArg);
5344-
if (checkBuiltinArgument(*this, TheCall, 0))
5344+
if (!FirstArgResult.get()->getType()->isPointerType()) {
5345+
Diag(TheCall->getBeginLoc(), diag::err_builtin_assume_aligned_invalid_arg)
5346+
<< TheCall->getSourceRange();
53455347
return true;
5346-
/// In-place updation of FirstArg by checkBuiltinArgument is ignored.
5348+
}
53475349
TheCall->setArg(0, FirstArgResult.get());
53485350
}
53495351

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -fsyntax-only -Wno-int-conversion -triple x86_64-linux -verify %s
2+
3+
// Check that the pointer->int conversion error is not downgradable for the
4+
// pointer argument to __builtin_assume_aligned.
5+
6+
int test(int *a, int b) {
7+
a = (int *)__builtin_assume_aligned(b, 32); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
8+
int *y = __builtin_assume_aligned(1, 1); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
9+
}

clang/test/Sema/builtin-assume-aligned.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int test13(int *a) {
7474
}
7575

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

8080
int test15(int *b) {

0 commit comments

Comments
 (0)