Skip to content

Commit 086183c

Browse files
committed
[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: #50534 Differential Revision: https://reviews.llvm.org/D152335
1 parent c01f284 commit 086183c

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ Bug Fixes in This Version
490490
(`#62886 <https://github.com/llvm/llvm-project/issues/62886>`_).
491491
- Fix crash for code using ``_Atomic`` types in C++
492492
(`See patch <https://reviews.llvm.org/D152303>`_).
493+
- Fix crash when passing a value larger then 64 bits to the aligned attribute.
494+
(`#50534 <https://github.com/llvm/llvm-project/issues/50534>`_).
493495

494496
Bug Fixes to Compiler Builtins
495497
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDeclAttr.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -4473,6 +4473,15 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
44734473
if (ICE.isInvalid())
44744474
return;
44754475

4476+
uint64_t MaximumAlignment = Sema::MaximumAlignment;
4477+
if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
4478+
MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
4479+
if (Alignment > MaximumAlignment) {
4480+
Diag(AttrLoc, diag::err_attribute_aligned_too_great)
4481+
<< MaximumAlignment << E->getSourceRange();
4482+
return;
4483+
}
4484+
44764485
uint64_t AlignVal = Alignment.getZExtValue();
44774486
// C++11 [dcl.align]p2:
44784487
// -- if the constant expression evaluates to zero, the alignment
@@ -4487,15 +4496,6 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
44874496
}
44884497
}
44894498

4490-
uint64_t MaximumAlignment = Sema::MaximumAlignment;
4491-
if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
4492-
MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
4493-
if (AlignVal > MaximumAlignment) {
4494-
Diag(AttrLoc, diag::err_attribute_aligned_too_great)
4495-
<< MaximumAlignment << E->getSourceRange();
4496-
return;
4497-
}
4498-
44994499
const auto *VD = dyn_cast<VarDecl>(D);
45004500
if (VD) {
45014501
unsigned MaxTLSAlign =

clang/test/Sema/attr-aligned.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
1+
// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -fsyntax-only -verify %s
22

33
int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}
44
int y __attribute__((aligned(1ull << 33))); // expected-error {{requested alignment must be 4294967296 bytes or smaller}}
55
int y __attribute__((aligned(1ull << 32)));
6+
// GH50534
7+
int z __attribute__((aligned((__int128_t)0x1234567890abcde0ULL << 64))); // expected-error {{requested alignment must be 4294967296 bytes or smaller}}
68

79
// PR26444
810
int y __attribute__((aligned(1 << 29)));

0 commit comments

Comments
 (0)