[PATCH] D136656: [IR Verifier] didn't check if switch case is constant, align IR Verifier's check with LLParser.
Peter Rong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 24 19:11:06 PDT 2022
Peter created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Peter requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
If a programmer incorrectly `Switch->setOperand()` and `Verifier` will pass, causing problems when dumping this `Module`
This patch aligns SwitchInst's check with LLParser. It also includes a test to justify the patch.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D136656
Files:
llvm/lib/IR/Verifier.cpp
llvm/unittests/IR/VerifierTest.cpp
Index: llvm/unittests/IR/VerifierTest.cpp
===================================================================
--- llvm/unittests/IR/VerifierTest.cpp
+++ llvm/unittests/IR/VerifierTest.cpp
@@ -270,5 +270,36 @@
EXPECT_TRUE(verifyFunction(*F2));
}
+TEST(VerifierTest, SwitchInst) {
+ LLVMContext C;
+ Module M("M", C);
+ IntegerType *Int32Ty = Type::getInt32Ty(C);
+ FunctionType *FTy =
+ FunctionType::get(Type::getVoidTy(C), {Int32Ty, Int32Ty},
+ /*isVarArg=*/false);
+ Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
+ BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
+ BasicBlock *Default = BasicBlock::Create(C, "default", F);
+ BasicBlock *OnOne = BasicBlock::Create(C, "on_one", F);
+ BasicBlock *OnTwo = BasicBlock::Create(C, "on_two", F);
+
+ BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
+
+ BranchInst::Create(Exit, Default);
+ BranchInst::Create(Exit, OnTwo);
+ BranchInst::Create(Exit, OnOne);
+ ReturnInst::Create(C, Exit);
+
+ Value *Cond = F->getArg(0);
+ SwitchInst *Switch = SwitchInst::Create(Cond, Default, 2, Entry);
+ Switch->addCase(ConstantInt::get(Int32Ty, 1), OnOne);
+ Switch->addCase(ConstantInt::get(Int32Ty, 2), OnTwo);
+
+ EXPECT_FALSE(verifyFunction(*F));
+ // set one case value to function argument.
+ Switch->setOperand(2, F->getArg(1));
+ EXPECT_TRUE(verifyFunction(*F));
+}
+
} // end anonymous namespace
} // end namespace llvm
Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -2879,6 +2879,8 @@
Type *SwitchTy = SI.getCondition()->getType();
SmallPtrSet<ConstantInt*, 32> Constants;
for (auto &Case : SI.cases()) {
+ Check(isa<ConstantInt>(SI.getOperand(Case.getCaseIndex() * 2 + 2)),
+ "Case value is not a constant integer.", &SI);
Check(Case.getCaseValue()->getType() == SwitchTy,
"Switch constants must all be same type as switch value!", &SI);
Check(Constants.insert(Case.getCaseValue()).second,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136656.470352.patch
Type: text/x-patch
Size: 2088 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221025/37505fe2/attachment.bin>
More information about the llvm-commits
mailing list