Fix for PR1633: Verifier doesn't fully verify GC intrinsics
authorGordon Henriksen <gordonhenriksen@mac.com>
Mon, 17 Sep 2007 20:30:04 +0000 (20:30 +0000)
committerGordon Henriksen <gordonhenriksen@mac.com>
Mon, 17 Sep 2007 20:30:04 +0000 (20:30 +0000)
LLVM now enforces the following prototypes for the write barriers:

<ty>* @llvm.gcread(<ty2>*, <ty>**)
void @llvm.gcwrite(<ty>*, <ty2>*, <ty>**)

And for @llvm.gcroot, the first stack slot is verified to be an alloca or a
bitcast of an alloca.

Fixes test/CodeGen/Generic/GC/lower_gcroot.ll, which violated these.

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

lib/VMCore/Verifier.cpp
test/CodeGen/Generic/GC/lower_gcroot.ll
test/Verifier/gcread-ptrptr.ll [new file with mode: 0644]
test/Verifier/gcroot-alloca.ll [new file with mode: 0644]
test/Verifier/gcroot-meta.ll [new file with mode: 0644]
test/Verifier/gcroot-ptrptr.ll [new file with mode: 0644]
test/Verifier/gcwrite-ptrptr.ll [new file with mode: 0644]

index 58bf485512a67f77432a038438b3fdf26216392d..25826e106cb26464fe37484271d19fb3cb4593f5 100644 (file)
@@ -1072,6 +1072,18 @@ void Verifier::visitInstruction(Instruction &I) {
   InstsInThisBlock.insert(&I);
 }
 
+static bool HasPtrPtrType(Value *Val) {
+  if (const PointerType *PtrTy = dyn_cast<PointerType>(Val->getType()))
+    return isa<PointerType>(PtrTy->getElementType());
+  return false;
+}
+
+static Value *StripBitCasts(Value *Val) {
+  if (BitCastInst *CI = dyn_cast<BitCastInst>(Val))
+    return StripBitCasts(CI->getOperand(0));
+  return Val;
+}
+
 /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
 ///
 void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
@@ -1082,6 +1094,30 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
 #define GET_INTRINSIC_VERIFIER
 #include "llvm/Intrinsics.gen"
 #undef GET_INTRINSIC_VERIFIER
+  
+  switch (ID) {
+  default:
+    break;
+  case Intrinsic::gcroot:
+    Assert1(HasPtrPtrType(CI.getOperand(1)),
+            "llvm.gcroot parameter #1 must be a pointer to a pointer.", &CI);
+    Assert1(isa<AllocaInst>(StripBitCasts(CI.getOperand(1))),
+            "llvm.gcroot parameter #1 must be an alloca (or a bitcast).", &CI);
+    Assert1(isa<Constant>(CI.getOperand(2)),
+            "llvm.gcroot parameter #2 must be a constant or global.", &CI);
+    break;
+  case Intrinsic::gcwrite:
+    Assert1(CI.getOperand(3)->getType()
+              == PointerType::get(CI.getOperand(1)->getType()),
+          "Call to llvm.gcwrite must be with type 'void (%ty*, %ty2*, %ty**)'.",
+            &CI);
+    break;
+  case Intrinsic::gcread:
+    Assert1(CI.getOperand(2)->getType() == PointerType::get(CI.getType()),
+            "Call to llvm.gcread must be with type '%ty* (%ty2*, %ty**).'",
+            &CI);
+    break;
+  }
 }
 
 /// VerifyIntrinsicPrototype - TableGen emits calls to this function into
index 3a3abac258544eeb10fafb28d05946095a4936de..2dbbc83569fd95f7f7ea8b1e63a9f4c9225376b1 100644 (file)
@@ -3,7 +3,8 @@
        %Env = type opaque*
 
 define void @.main(%Env) {
-       call void @llvm.gcroot( %Env* null, %Env null )
+       %Root = alloca %Env
+       call void @llvm.gcroot( %Env* %Root, %Env null )
        unreachable
 }
 
diff --git a/test/Verifier/gcread-ptrptr.ll b/test/Verifier/gcread-ptrptr.ll
new file mode 100644 (file)
index 0000000..bcfc6eb
--- /dev/null
@@ -0,0 +1,13 @@
+; RUN: not llvm-as < %s
+; PR1633
+
+%meta = type { i8* }
+%obj = type { %meta* }
+
+declare %obj* @llvm.gcread(%obj*, %obj*)
+
+define %obj* @f() {
+entry:
+       %x = call %obj* @llvm.gcread(%obj* null, %obj* null)
+       ret %obj* %x
+}
diff --git a/test/Verifier/gcroot-alloca.ll b/test/Verifier/gcroot-alloca.ll
new file mode 100644 (file)
index 0000000..c44321a
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: not llvm-as < %s
+; PR1633
+
+%meta = type { i8* }
+%obj = type { %meta* }
+
+declare void @llvm.gcroot(%obj**, %meta*)
+
+define void @f() {
+entry:
+       call void @llvm.gcroot(%obj** null, %meta* null)
+       
+       ret void
+}
diff --git a/test/Verifier/gcroot-meta.ll b/test/Verifier/gcroot-meta.ll
new file mode 100644 (file)
index 0000000..51d1951
--- /dev/null
@@ -0,0 +1,16 @@
+; RUN: not llvm-as < %s
+; PR1633
+
+%meta = type { i8* }
+%obj = type { %meta* }
+
+declare void @llvm.gcroot(%obj**, %meta*)
+
+define void @f() {
+entry:
+       %local.obj = alloca %obj*
+       %local.meta = alloca %meta
+       call void @llvm.gcroot(%obj** %local.obj, %meta* %local.meta)
+       
+       ret void
+}
diff --git a/test/Verifier/gcroot-ptrptr.ll b/test/Verifier/gcroot-ptrptr.ll
new file mode 100644 (file)
index 0000000..0a7738d
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: not llvm-as < %s
+; PR1633
+
+%meta = type { i8* }
+%obj = type { %meta* }
+
+declare void @llvm.gcroot(%obj*, %meta*)
+
+define void @f() {
+entry:
+       %local.obj = alloca %obj
+       call void @llvm.gcroot(%obj* %local.obj, %meta* null)
+       ret void
+}
diff --git a/test/Verifier/gcwrite-ptrptr.ll b/test/Verifier/gcwrite-ptrptr.ll
new file mode 100644 (file)
index 0000000..b1e96c8
--- /dev/null
@@ -0,0 +1,13 @@
+; RUN: not llvm-as < %s
+; PR1633
+
+%meta = type { i8* }
+%obj = type { %meta* }
+
+declare void @llvm.gcwrite(%obj*, %obj*, %obj*)
+
+define void @f() {
+entry:
+       call void @llvm.gcwrite(%obj* null, %obj* null, %obj* null)
+       ret void
+}