Fix PR3746 - Crash in isel with GEP of function pointer

by checking that the top-level type of a gep is sized. This
causes us to reject the example with:

llvm-as: t2.ll:2:16: invalid getelementptr indices
getelementptr i32()* null, i32 1
              ^

llvm-svn: 66393
This commit is contained in:
Chris Lattner 2009-03-09 04:46:40 +00:00
parent 3b935fbcf2
commit 6090a42fe5
1 changed files with 17 additions and 13 deletions

View File

@ -1037,27 +1037,31 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
init(Ptr, Idx, Name);
}
// getIndexedType - Returns the type of the element that would be loaded with
// a load instruction with the specified parameters.
//
// The Idxs pointer should point to a continuous piece of memory containing the
// indices, either as Value* or uint64_t.
//
// A null type is returned if the indices are invalid for the specified
// pointer type.
//
/// getIndexedType - Returns the type of the element that would be accessed with
/// a gep instruction with the specified parameters.
///
/// The Idxs pointer should point to a continuous piece of memory containing the
/// indices, either as Value* or uint64_t.
///
/// A null type is returned if the indices are invalid for the specified
/// pointer type.
///
template <typename IndexTy>
static const Type* getIndexedTypeInternal(const Type *Ptr,
IndexTy const *Idxs,
static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
unsigned NumIdx) {
const PointerType *PTy = dyn_cast<PointerType>(Ptr);
if (!PTy) return 0; // Type isn't a pointer type!
const Type *Agg = PTy->getElementType();
// Handle the special case of the empty set index set...
// Handle the special case of the empty set index set, which is always valid.
if (NumIdx == 0)
return Agg;
// If there is at least one index, the top level type must be sized, otherwise
// it cannot be 'stepped over'.
if (!Agg->isSized())
return 0;
unsigned CurIdx = 1;
for (; CurIdx != NumIdx; ++CurIdx) {
const CompositeType *CT = dyn_cast<CompositeType>(Agg);