[PATCH] D37894: [Lint] Avoid failed assertion by fetching the proper pointer type

Mikael Holmén via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 01:32:26 PDT 2017


uabelho created this revision.
Herald added a subscriber: wdng.

When checking if a constant expression is a noop cast we fetched the
IntPtrType by doing DL->getIntPtrType(V->getType())). However, there can
be cases where V doesn't return a pointer, and then getIntPtrType()
triggers an assertion.

Now we examine the constant expression a bit more to determine the
IntPtrType similar to what the DataLayout version of CastInst::isNoopCast
does.


https://reviews.llvm.org/D37894

Files:
  lib/Analysis/Lint.cpp
  test/Analysis/Lint/noop-cast-expr-no-pointer.ll


Index: test/Analysis/Lint/noop-cast-expr-no-pointer.ll
===================================================================
--- /dev/null
+++ test/Analysis/Lint/noop-cast-expr-no-pointer.ll
@@ -0,0 +1,23 @@
+; RUN: opt -lint < %s
+
+; lint shouldn't crash on any of the below functions
+
+ at g_1 = external global [3 x i32]
+ at g_2 = external global [2 x i32]
+
+define void @test1() {
+entry:
+  tail call void @f1(i16 zext (i1 icmp eq (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @g_2, i64 0, i64 0), i32* getelementptr inbounds ([3 x i32], [3 x i32]* @g_1, i64 0, i64 1)) to i16))
+  ret void
+}
+
+declare void @f1(i16)
+
+define void @test2() {
+  tail call void inttoptr (i64 sext (i32 ptrtoint (void ()* @f2 to i32) to i64) to void ()*)()
+
+  ret void
+}
+
+declare void @f2()
+
Index: lib/Analysis/Lint.cpp
===================================================================
--- lib/Analysis/Lint.cpp
+++ lib/Analysis/Lint.cpp
@@ -681,9 +681,19 @@
   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     // Same as above, but for ConstantExpr instead of Instruction.
     if (Instruction::isCast(CE->getOpcode())) {
+      // Fetch the proper IntPtrTy. V/CE might be of non-pointer type so we
+      // can't always call getIntPtrType() on its return type.
+      Type *PtrOpTy = nullptr;
+      if (CE->getOpcode() == Instruction::PtrToInt)
+        PtrOpTy = CE->getOperand(0)->getType();
+      else if (CE->getOpcode() == Instruction::IntToPtr)
+        PtrOpTy = CE->getType();
+      Type *IntPtrTy = PtrOpTy ? DL->getIntPtrType(PtrOpTy) :
+                                 DL->getIntPtrType(CE->getContext(), 0);
+
       if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
                                CE->getOperand(0)->getType(), CE->getType(),
-                               DL->getIntPtrType(V->getType())))
+                               IntPtrTy))
         return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
     } else if (CE->getOpcode() == Instruction::ExtractValue) {
       ArrayRef<unsigned> Indices = CE->getIndices();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37894.115371.patch
Type: text/x-patch
Size: 2088 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170915/1e363f21/attachment.bin>


More information about the llvm-commits mailing list