Don't use isa<CallInst>(this) in the constructor for CallInst's base class.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 20 Dec 2012 04:11:02 +0000 (04:11 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 20 Dec 2012 04:11:02 +0000 (04:11 +0000)
This has undefined behavior, because the classof implementation attempts to
access parts of the not-yet-constructed derived class. Found by clang
-fsanitize=vptr.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170658 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Value.cpp

index b10e093c152cb107adc6a5ad5584375a25f95739..04ae441513805b8e50fa73d3c587e6fe38db43c1 100644 (file)
@@ -46,10 +46,13 @@ Value::Value(Type *ty, unsigned scid)
     SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)),
     UseList(0), Name(0) {
   // FIXME: Why isn't this in the subclass gunk??
-  if (isa<CallInst>(this) || isa<InvokeInst>(this))
+  // Note, we cannot call isa<CallInst> before the CallInst has been
+  // constructed.
+  if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
     assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
            "invalid CallInst type!");
-  else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
+  else if (SubclassID != BasicBlockVal &&
+           (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
     assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
            "Cannot create non-first-class values except for constants!");
 }