[clang] 086183c - [Clang] Add check to Sema::AddAlignedAttr to verify active bits is not out of range

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 8 13:54:28 PDT 2023


Author: Shafik Yaghmour
Date: 2023-06-08T13:53:33-07:00
New Revision: 086183c6c65fc8106b8cae99af95c4974b975f51

URL: https://github.com/llvm/llvm-project/commit/086183c6c65fc8106b8cae99af95c4974b975f51
DIFF: https://github.com/llvm/llvm-project/commit/086183c6c65fc8106b8cae99af95c4974b975f51.diff

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

If we provide too large a value for the alignment attribute
APInt::getZExtValue() will assert. This PR adjusts existing check to catch
this case and provide a diagnostic.

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

Differential Revision: https://reviews.llvm.org/D152335

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclAttr.cpp
    clang/test/Sema/attr-aligned.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0ae2be88e7a42..e3dd7910e7a9e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -490,6 +490,8 @@ Bug Fixes in This Version
   (`#62886 <https://github.com/llvm/llvm-project/issues/62886>`_).
 - Fix crash for code using ``_Atomic`` types in C++
   (`See patch <https://reviews.llvm.org/D152303>`_).
+- Fix crash when passing a value larger then 64 bits to the aligned attribute.
+  (`#50534 <https://github.com/llvm/llvm-project/issues/50534>`_).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1e48d078a61bb..40438214d2b46 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -4473,6 +4473,15 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
   if (ICE.isInvalid())
     return;
 
+  uint64_t MaximumAlignment = Sema::MaximumAlignment;
+  if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
+    MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
+  if (Alignment > MaximumAlignment) {
+    Diag(AttrLoc, diag::err_attribute_aligned_too_great)
+        << MaximumAlignment << E->getSourceRange();
+    return;
+  }
+
   uint64_t AlignVal = Alignment.getZExtValue();
   // C++11 [dcl.align]p2:
   //   -- if the constant expression evaluates to zero, the alignment
@@ -4487,15 +4496,6 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
     }
   }
 
-  uint64_t MaximumAlignment = Sema::MaximumAlignment;
-  if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
-    MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
-  if (AlignVal > MaximumAlignment) {
-    Diag(AttrLoc, diag::err_attribute_aligned_too_great)
-        << MaximumAlignment << E->getSourceRange();
-    return;
-  }
-
   const auto *VD = dyn_cast<VarDecl>(D);
   if (VD) {
     unsigned MaxTLSAlign =

diff  --git a/clang/test/Sema/attr-aligned.c b/clang/test/Sema/attr-aligned.c
index 87580c7020ccf..039dff085b79e 100644
--- a/clang/test/Sema/attr-aligned.c
+++ b/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)));


        


More information about the cfe-commits mailing list