Skip to content

Commit

Permalink
[Attributes] Assert correct attribute constructor is used (NFCI)
Browse files Browse the repository at this point in the history
Assert that enum/int/type attributes go through the constructor
they are supposed to use.

To make sure this can't happen via invalid bitcode, explicitly
verify that the attribute kind if correct there.
  • Loading branch information
nikic committed Jul 12, 2021
1 parent b4a6fa1 commit 3fb0621
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/Attributes.h
Expand Up @@ -80,6 +80,12 @@ class Attribute {

static const unsigned NumTypeAttrKinds = LastTypeAttr - FirstTypeAttr + 1;

static bool isEnumAttrKind(AttrKind Kind) {
return Kind >= FirstEnumAttr && Kind <= LastEnumAttr;
}
static bool isIntAttrKind(AttrKind Kind) {
return Kind >= FirstIntAttr && Kind <= LastIntAttr;
}
static bool isTypeAttrKind(AttrKind Kind) {
return Kind >= FirstTypeAttr && Kind <= LastTypeAttr;
}
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Expand Up @@ -1599,12 +1599,16 @@ Error BitcodeReader::parseAttributeGroupBlock() {
B.addStructRetAttr(nullptr);
else if (Kind == Attribute::InAlloca)
B.addInAllocaAttr(nullptr);

B.addAttribute(Kind);
else if (Attribute::isEnumAttrKind(Kind))
B.addAttribute(Kind);
else
return error("Not an enum attribute");
} else if (Record[i] == 1) { // Integer attribute
Attribute::AttrKind Kind;
if (Error Err = parseAttrKind(Record[++i], &Kind))
return Err;
if (!Attribute::isIntAttrKind(Kind))
return error("Not an int attribute");
if (Kind == Attribute::Alignment)
B.addAlignmentAttr(Record[++i]);
else if (Kind == Attribute::StackAlignment)
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/IR/Attributes.cpp
Expand Up @@ -91,6 +91,11 @@ static std::pair<unsigned, unsigned> unpackVScaleRangeArgs(uint64_t Value) {

Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
uint64_t Val) {
if (Val)
assert(Attribute::isIntAttrKind(Kind) && "Not an int attribute");
else
assert(Attribute::isEnumAttrKind(Kind) && "Not an enum attribute");

LLVMContextImpl *pImpl = Context.pImpl;
FoldingSetNodeID ID;
ID.AddInteger(Kind);
Expand Down

0 comments on commit 3fb0621

Please sign in to comment.