[llvm] fad8d42 - [OpaquePtr] Verify Opaque pointer in function parameter

Zequan Wu via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 15 14:57:58 PDT 2021


Author: Zequan Wu
Date: 2021-06-15T14:57:48-07:00
New Revision: fad8d4230ff73432b73f8111f5d640c6bb014daa

URL: https://github.com/llvm/llvm-project/commit/fad8d4230ff73432b73f8111f5d640c6bb014daa
DIFF: https://github.com/llvm/llvm-project/commit/fad8d4230ff73432b73f8111f5d640c6bb014daa.diff

LOG: [OpaquePtr] Verify Opaque pointer in function parameter

Verifying opaque pointer as function parameter when using with `byval`, `byref`,
`inalloca`, `preallocated`.

Differential Revision: https://reviews.llvm.org/D104309

Added: 
    llvm/test/Verifier/opaque-ptr-invalid.ll

Modified: 
    llvm/lib/IR/Verifier.cpp
    llvm/test/Assembler/invalid-byval-type3.ll
    llvm/test/Verifier/byref.ll
    llvm/test/Verifier/inalloca1.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 6ff4a398449c4..bfdfb7a61d495 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1839,40 +1839,51 @@ void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
          V);
 
   if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
-    SmallPtrSet<Type*, 4> Visited;
-    if (!PTy->getElementType()->isSized(&Visited)) {
-      Assert(!Attrs.hasAttribute(Attribute::ByVal) &&
-             !Attrs.hasAttribute(Attribute::ByRef) &&
-             !Attrs.hasAttribute(Attribute::InAlloca) &&
-             !Attrs.hasAttribute(Attribute::Preallocated),
-             "Attributes 'byval', 'byref', 'inalloca', and 'preallocated' do not "
-             "support unsized types!",
-             V);
+    if (Attrs.hasAttribute(Attribute::ByVal)) {
+      SmallPtrSet<Type *, 4> Visited;
+      Assert(Attrs.getByValType()->isSized(&Visited),
+             "Attribute 'byval' does not support unsized types!", V);
     }
-    if (!isa<PointerType>(PTy->getElementType()))
-      Assert(!Attrs.hasAttribute(Attribute::SwiftError),
-             "Attribute 'swifterror' only applies to parameters "
-             "with pointer to pointer type!",
-             V);
-
     if (Attrs.hasAttribute(Attribute::ByRef)) {
-      Assert(Attrs.getByRefType() == PTy->getElementType(),
-             "Attribute 'byref' type does not match parameter!", V);
+      SmallPtrSet<Type *, 4> Visited;
+      Assert(Attrs.getByRefType()->isSized(&Visited),
+             "Attribute 'byref' does not support unsized types!", V);
     }
-
-    if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
-      Assert(Attrs.getByValType() == PTy->getElementType(),
-             "Attribute 'byval' type does not match parameter!", V);
+    if (Attrs.hasAttribute(Attribute::InAlloca)) {
+      SmallPtrSet<Type *, 4> Visited;
+      Assert(Attrs.getInAllocaType()->isSized(&Visited),
+             "Attribute 'inalloca' does not support unsized types!", V);
     }
-
     if (Attrs.hasAttribute(Attribute::Preallocated)) {
-      Assert(Attrs.getPreallocatedType() == PTy->getElementType(),
-             "Attribute 'preallocated' type does not match parameter!", V);
+      SmallPtrSet<Type *, 4> Visited;
+      Assert(Attrs.getPreallocatedType()->isSized(&Visited),
+             "Attribute 'preallocated' does not support unsized types!", V);
     }
+    if (!PTy->isOpaque()) {
+      if (!isa<PointerType>(PTy->getElementType()))
+        Assert(!Attrs.hasAttribute(Attribute::SwiftError),
+               "Attribute 'swifterror' only applies to parameters "
+               "with pointer to pointer type!",
+               V);
+      if (Attrs.hasAttribute(Attribute::ByRef)) {
+        Assert(Attrs.getByRefType() == PTy->getElementType(),
+               "Attribute 'byref' type does not match parameter!", V);
+      }
 
-    if (Attrs.hasAttribute(Attribute::InAlloca)) {
-      Assert(Attrs.getInAllocaType() == PTy->getElementType(),
-             "Attribute 'inalloca' type does not match parameter!", V);
+      if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
+        Assert(Attrs.getByValType() == PTy->getElementType(),
+               "Attribute 'byval' type does not match parameter!", V);
+      }
+
+      if (Attrs.hasAttribute(Attribute::Preallocated)) {
+        Assert(Attrs.getPreallocatedType() == PTy->getElementType(),
+               "Attribute 'preallocated' type does not match parameter!", V);
+      }
+
+      if (Attrs.hasAttribute(Attribute::InAlloca)) {
+        Assert(Attrs.getInAllocaType() == PTy->getElementType(),
+               "Attribute 'inalloca' type does not match parameter!", V);
+      }
     }
   } else {
     Assert(!Attrs.hasAttribute(Attribute::ByVal),

diff  --git a/llvm/test/Assembler/invalid-byval-type3.ll b/llvm/test/Assembler/invalid-byval-type3.ll
index 5263e03d45dee..b62fb5354a5b2 100644
--- a/llvm/test/Assembler/invalid-byval-type3.ll
+++ b/llvm/test/Assembler/invalid-byval-type3.ll
@@ -1,4 +1,4 @@
 ; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
 
-; CHECK: Attributes 'byval'{{.*}} do not support unsized types!
+; CHECK: Attribute 'byval' does not support unsized types!
 declare void @foo(void()* byval(void()))

diff  --git a/llvm/test/Verifier/byref.ll b/llvm/test/Verifier/byref.ll
index d5921bf5b2614..6cd66eb60e5a6 100644
--- a/llvm/test/Verifier/byref.ll
+++ b/llvm/test/Verifier/byref.ll
@@ -14,7 +14,7 @@ define void @byref_mismatched_pointee_type1(i8* byref(i32)) {
 
 %opaque.ty = type opaque
 
-; CHECK: Attributes 'byval', 'byref', 'inalloca', and 'preallocated' do not support unsized types!
+; CHECK: Attribute 'byref' does not support unsized types!
 ; CHECK-NEXT: void (%opaque.ty*)* @byref_unsized
 define void @byref_unsized(%opaque.ty* byref(%opaque.ty)) {
   ret void

diff  --git a/llvm/test/Verifier/inalloca1.ll b/llvm/test/Verifier/inalloca1.ll
index 76da66adc7983..db911b2d5f60a 100644
--- a/llvm/test/Verifier/inalloca1.ll
+++ b/llvm/test/Verifier/inalloca1.ll
@@ -16,7 +16,7 @@ declare void @e(i64* readonly inalloca(i64) %p)
 ; CHECK: Attributes {{.*}} are incompatible
 
 declare void @f(void ()* inalloca(void()) %p)
-; CHECK: do not support unsized types
+; CHECK: Attribute 'inalloca' does not support unsized types
 
 declare void @g(i32* inalloca(i32) %p, i32 %p2)
 ; CHECK: inalloca isn't on the last parameter!

diff  --git a/llvm/test/Verifier/opaque-ptr-invalid.ll b/llvm/test/Verifier/opaque-ptr-invalid.ll
new file mode 100644
index 0000000000000..18a4a64a15581
--- /dev/null
+++ b/llvm/test/Verifier/opaque-ptr-invalid.ll
@@ -0,0 +1,7 @@
+; RUN: not opt -verify < %s 2>&1 | FileCheck %s
+
+; CHECK: Attribute 'inalloca' does not support unsized types!
+; CHECK-NEXT: void (ptr)* @f
+define void @f(ptr inalloca(token)) {
+    ret void
+}


        


More information about the llvm-commits mailing list