[PATCH] D152335: [Clang] Add check to Sema::AddAlignedAttr to verify active bits is not out of range

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 6 20:04:36 PDT 2023


shafik created this revision.
shafik added reviewers: aaron.ballman, erichkeane.
Herald added a project: All.
shafik requested review of this revision.

If we provide too large a value for the alignment attribute `APInt::getZExtValue()` will assert. This PR adds a active bits check and folds it into the `MaximumAlignment` check.

This fixes: https://github.com/llvm/llvm-project/issues/50534


https://reviews.llvm.org/D152335

Files:
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Sema/attr-aligned.c


Index: clang/test/Sema/attr-aligned.c
===================================================================
--- clang/test/Sema/attr-aligned.c
+++ clang/test/Sema/attr-aligned.c
@@ -1,8 +1,10 @@
-// RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -fsyntax-only -verify %s
 
 int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}
 int y __attribute__((aligned(1ull << 33))); // expected-error {{requested alignment must be 4294967296 bytes or smaller}}
 int y __attribute__((aligned(1ull << 32)));
+// GH50534
+int z __attribute__((aligned((__int128_t)0x1234567890abcde0ULL << 64))); // expected-error {{requested alignment must be 4294967296 bytes or smaller}}
 
 // PR26444
 int y __attribute__((aligned(1 << 29)));
Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -4473,24 +4473,30 @@
   if (ICE.isInvalid())
     return;
 
-  uint64_t AlignVal = Alignment.getZExtValue();
-  // C++11 [dcl.align]p2:
-  //   -- if the constant expression evaluates to zero, the alignment
-  //      specifier shall have no effect
-  // C11 6.7.5p6:
-  //   An alignment specification of zero has no effect.
-  if (!(TmpAttr.isAlignas() && !Alignment)) {
-    if (!llvm::isPowerOf2_64(AlignVal)) {
-      Diag(AttrLoc, diag::err_alignment_not_power_of_two)
-        << E->getSourceRange();
-      return;
-    }
-  }
 
   uint64_t MaximumAlignment = Sema::MaximumAlignment;
   if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
     MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
-  if (AlignVal > MaximumAlignment) {
+  bool TooManyActiveBits = Alignment.getActiveBits() > llvm::APInt(64, MaximumAlignment).getActiveBits();
+
+  uint64_t AlignVal = 0;
+  if (!TooManyActiveBits) {
+    AlignVal = Alignment.getZExtValue();
+    // C++11 [dcl.align]p2:
+    //   -- if the constant expression evaluates to zero, the alignment
+    //      specifier shall have no effect
+    // C11 6.7.5p6:
+    //   An alignment specification of zero has no effect.
+    if (!(TmpAttr.isAlignas() && !Alignment)) {
+      if (!llvm::isPowerOf2_64(AlignVal)) {
+        Diag(AttrLoc, diag::err_alignment_not_power_of_two)
+          << E->getSourceRange();
+        return;
+      }
+    }
+  }
+
+  if (TooManyActiveBits || AlignVal > MaximumAlignment) {
     Diag(AttrLoc, diag::err_attribute_aligned_too_great)
         << MaximumAlignment << E->getSourceRange();
     return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152335.529141.patch
Type: text/x-patch
Size: 2617 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230607/2d7c5bac/attachment-0001.bin>


More information about the cfe-commits mailing list