This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by shafik on Jun 6 2023, 8:04 PM.

Details

Summary

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

Diff Detail

Event Timeline

shafik created this revision.Jun 6 2023, 8:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 8:04 PM
shafik requested review of this revision.Jun 6 2023, 8:04 PM
erichkeane added inline comments.Jun 7 2023, 6:28 AM
clang/lib/Sema/SemaDeclAttr.cpp
4498–4499

What is the purpose of using 'ActiveBits' here? Why is this not a sub-set of the operator > from below? That is, 'ActiveBits' allows for 0xFF when the 'MaxValue' is 0x80 (top bit set), but the operator > would have caught that anyway, right?

Also, how does 'ActiveBits' work with negative numbers?

erichkeane added inline comments.Jun 7 2023, 6:40 AM
clang/lib/Sema/SemaDeclAttr.cpp
4502

So looking more closely, THIS is the problem right here. I think we probably should just be storing MaximumAlignment and AlignVal as an llvm::APInt and just do the comparison on APInt, rather than try to be tricky with the bits here.

Basically, don't extract the 'Alignment' from the APInt until after the test for >.

aaron.ballman added inline comments.Jun 7 2023, 9:27 AM
clang/lib/Sema/SemaDeclAttr.cpp
4502

You're correct that this is the cause of the assertion, which happens when Alignment can't fit in a uint64_t.

It took me a minute to see what you meant, but I agree, we can test whether Alignment < MaximumAlignment and rely on the fact that MaximumAlignment will be representable in a 64-bit integer. I think I led @shafik astray with a comment I made on the issue.

shafik updated this revision to Diff 529396.Jun 7 2023, 12:04 PM
shafik marked 2 inline comments as done.
  • Switched to using APSInt::operator> as discussed offline w/ Erich and Aaron
shafik marked an inline comment as done.Jun 7 2023, 12:05 PM
shafik added inline comments.
clang/test/Sema/attr-aligned.c
1

The previous triple did not support __int128_t

erichkeane accepted this revision.Jun 7 2023, 12:09 PM

Needs a release note. Also, the changing of order is POSSIBLY a change in behavior (in that we're now diagnosing 'out of range' before 'power of 2'), but I don't think we care.

This revision is now accepted and ready to land.Jun 7 2023, 12:09 PM
aaron.ballman accepted this revision.Jun 7 2023, 12:12 PM

LGTM with a release note.

Needs a release note. Also, the changing of order is POSSIBLY a change in behavior (in that we're now diagnosing 'out of range' before 'power of 2'), but I don't think we care.

I don't consider that a behavioral change to be worried about. Telling the user "this value is too big" before telling them "it's not a power of two" seems equivalent or perhaps slightly better than "this is not a power of two" and then "oh, and it's too big."

LGTM with a release note.

Needs a release note. Also, the changing of order is POSSIBLY a change in behavior (in that we're now diagnosing 'out of range' before 'power of 2'), but I don't think we care.

I don't consider that a behavioral change to be worried about. Telling the user "this value is too big" before telling them "it's not a power of two" seems equivalent or perhaps slightly better than "this is not a power of two" and then "oh, and it's too big."

Well, its telling them "this value is too big" instead of "its not a power of two" rather than the other way around. But yeah, i don't think I care? I'm a little frightened there is no test that fails because of this, but not surprised.

shafik updated this revision to Diff 529714.Jun 8 2023, 1:44 PM
  • Add release note
erichkeane accepted this revision.Jun 8 2023, 1:47 PM
This revision was landed with ongoing or failed builds.Jun 8 2023, 1:54 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2023, 1:54 PM