[clang] [Clang] Reject non-scalar pointee types in masked builtins (PR #247474)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 00:01:38 PST 2026


https://github.com/YzK0741 created https://github.com/llvm/llvm-project/pull/247474

A non-scalar pointee (e.g. int **) passed the check in CheckMaskedBuiltinArgs, was then passed to getExtVectorType, and triggered an assertion, crashing clang.

CheckMaskedBuiltinArgs now allows only pointee types that getExtVectorType accepts (builtin, dependent, or a _BitInt with a power-of-two bit width) and reports an error otherwise. The check is shared by __builtin_masked_load, __builtin_masked_store, __builtin_masked_gather and __builtin_masked_scatter.

Tests covering all four builtins are added in clang/test/Sema/builtin-masked.c; llvm-lit passes locally.

Fixes #246852

>From 469fa790dcdb860fd4e0bd51a1c5a9b9a1440628 Mon Sep 17 00:00:00 2001
From: YzK0741 <2353036486 at qq.com>
Date: Tue, 1 Dec 2026 15:37:21 +0800
Subject: [PATCH] [Clang] Reject non-scalar pointee types in masked builtins

A non-scalar pointee (e.g. int **) passed the check in
CheckMaskedBuiltinArgs, was then passed to getExtVectorType, and triggered
an assertion, crashing clang.

CheckMaskedBuiltinArgs now allows only pointee types that getExtVectorType
accepts (builtin, dependent, or a _BitInt with a power-of-two bit width) and
reports an error otherwise. The check is shared by __builtin_masked_load,
__builtin_masked_store, __builtin_masked_gather and
__builtin_masked_scatter.

Fixes #246852
---
 clang/lib/Sema/SemaChecking.cpp  | 19 ++++++++++++++++++-
 clang/test/Sema/builtin-masked.c |  7 +++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index bbdd6f4ac9a40b..ab42aa9a67f02d 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2863,11 +2863,15 @@ static bool CheckMaskedBuiltinArgs(Sema &S, Expr *MaskArg, Expr *PtrArg,
            << MaskTy;
 
   QualType PtrTy = PtrArg->getType();
-  if (!PtrTy->isPointerType() || PtrTy->getPointeeType()->isVectorType())
+  if (!PtrTy->isPointerType())
     return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
            << Pos << "scalar pointer";
 
   QualType PointeeTy = PtrTy->getPointeeType();
+  if (PointeeTy->isVectorType())
+    return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
+           << Pos << "scalar pointer";
+
   if (PointeeTy.isVolatileQualified() || PointeeTy->isAtomicType() ||
       (!AllowConst && PointeeTy.isConstQualified()) ||
       (!AllowAS && PointeeTy.hasAddressSpace())) {
@@ -2879,6 +2883,19 @@ static bool CheckMaskedBuiltinArgs(Sema &S, Expr *MaskArg, Expr *PtrArg,
            << /*qualifier difference=*/0 << /*parameter mismatch=*/3 << 2
            << PtrTy << Target;
   }
+
+  bool IsValidElem = PointeeTy->isBuiltinType() || PointeeTy->isDependentType();
+
+  if (!IsValidElem && PointeeTy->isBitIntType()) {
+    unsigned Bits = PointeeTy->castAs<BitIntType>()->getNumBits();
+    if (llvm::isPowerOf2_32(Bits)) {
+      IsValidElem = true;
+    }
+  }
+
+  if (!IsValidElem)
+    return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
+           << Pos << "scalar pointer";
   return false;
 }
 
diff --git a/clang/test/Sema/builtin-masked.c b/clang/test/Sema/builtin-masked.c
index af555785aca7aa..3311c3236081e1 100644
--- a/clang/test/Sema/builtin-masked.c
+++ b/clang/test/Sema/builtin-masked.c
@@ -69,6 +69,13 @@ void c(v8b mask, const v8i v, int *ptr) {
   __builtin_masked_store(mask, v, ptr);
 }
 
+void d(v8b mask, v8i idx, const int *ptr) {
+  __builtin_masked_load(mask, (int **)ptr); // expected-error {{2nd argument must be a scalar pointer}}
+  __builtin_masked_store(mask, idx, (int **)ptr); // expected-error {{3rd argument must be a scalar pointer}}
+  __builtin_masked_gather(mask, idx, (int **)ptr); // expected-error {{3rd argument must be a scalar pointer}}
+  __builtin_masked_scatter(mask, idx, idx, (int **)ptr); // expected-error {{4th argument must be a scalar pointer}}
+}
+
 void readonly(v8b mask, v8i v, const int *ptr, const int *s) {
   (void)__builtin_masked_store(mask, v, ptr); // expected-error {{sending 'const int *' to parameter of incompatible type 'int *': type mismatch at 2nd parameter ('const int *' vs 'int *')}}
   (void)__builtin_masked_compress_store(mask, v, ptr); // expected-error {{sending 'const int *' to parameter of incompatible type 'int *': type mismatch at 2nd parameter ('const int *' vs 'int *')}}



More information about the cfe-commits mailing list