From 3fb0621fafbb16ea157830b308c26a77ba0a04e3 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 12 Jul 2021 20:54:58 +0200 Subject: [PATCH] [Attributes] Assert correct attribute constructor is used (NFCI) 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. --- llvm/include/llvm/IR/Attributes.h | 6 ++++++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 8 ++++++-- llvm/lib/IR/Attributes.cpp | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h index 4c2a366..2f04c02 100644 --- a/llvm/include/llvm/IR/Attributes.h +++ b/llvm/include/llvm/IR/Attributes.h @@ -80,6 +80,12 @@ public: 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; } diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 585527f..c9c1cef3 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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) diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 45f7f18..3c3ffea 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -91,6 +91,11 @@ static std::pair 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); -- 2.7.4