[llvm] r230188 - AsmParser: Check ConstantExpr GEP operands for validity
David Majnemer
david.majnemer at gmail.com
Sun Feb 22 15:14:53 PST 2015
Author: majnemer
Date: Sun Feb 22 17:14:52 2015
New Revision: 230188
URL: http://llvm.org/viewvc/llvm-project?rev=230188&view=rev
Log:
AsmParser: Check ConstantExpr GEP operands for validity
Added:
llvm/trunk/test/Assembler/getelementptr_vec_idx4.ll
Modified:
llvm/trunk/lib/AsmParser/LLParser.cpp
Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=230188&r1=230187&r2=230188&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Sun Feb 22 17:14:52 2015
@@ -2776,11 +2776,33 @@ bool LLParser::ParseValID(ValID &ID, Per
if (Opc == Instruction::GetElementPtr) {
if (Elts.size() == 0 ||
!Elts[0]->getType()->getScalarType()->isPointerTy())
- return Error(ID.Loc, "getelementptr requires pointer operand");
+ return Error(ID.Loc, "base of getelementptr must be a pointer");
+
+ Type *BaseType = Elts[0]->getType();
+ auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
+ for (Constant *Val : Indices) {
+ Type *ValTy = Val->getType();
+ if (!ValTy->getScalarType()->isIntegerTy())
+ return Error(ID.Loc, "getelementptr index must be an integer");
+ if (ValTy->isVectorTy() != BaseType->isVectorTy())
+ return Error(ID.Loc, "getelementptr index type missmatch");
+ if (ValTy->isVectorTy()) {
+ unsigned ValNumEl = cast<VectorType>(ValTy)->getNumElements();
+ unsigned PtrNumEl = cast<VectorType>(BaseType)->getNumElements();
+ if (ValNumEl != PtrNumEl)
+ return Error(
+ ID.Loc,
+ "getelementptr vector index has a wrong number of elements");
+ }
+ }
+
+ if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
+ return Error(ID.Loc, "base element of getelementptr must be sized");
+
if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
- return Error(ID.Loc, "invalid indices for getelementptr");
+ return Error(ID.Loc, "invalid getelementptr indices");
ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
InBounds);
} else if (Opc == Instruction::Select) {
Added: llvm/trunk/test/Assembler/getelementptr_vec_idx4.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/getelementptr_vec_idx4.ll?rev=230188&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/getelementptr_vec_idx4.ll (added)
+++ llvm/trunk/test/Assembler/getelementptr_vec_idx4.ll Sun Feb 22 17:14:52 2015
@@ -0,0 +1,5 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+; CHECK: getelementptr vector index has a wrong number of elements
+
+global <2 x i32*> getelementptr (<4 x [3 x {i32, i32}]*> zeroinitializer, <2 x i32> <i32 1, i32 2>, <2 x i32> <i32 2, i32 3>, <2 x i32> <i32 1, i32 1>)
More information about the llvm-commits
mailing list