[PATCH] D57052: [IPCP] Don't crash due to arg count mismath between caller/callee

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 22 06:22:16 PST 2019


bjope created this revision.
bjope added reviewers: jdoerfert, reames.

This patch avoids an assert in IPConstantPropagation when
there is a argument count mismatch between the caller and the
callee.

While this is actually UB on C-level (clang emits a warning),
the IR verifier seems to accept it. I'm not sure what other
frontends/languages might think about this, so simply bailing out
to avoid hitting an assert (in CallSiteBase<>::getArgOperand)
seems like a simple solution.


Repository:
  rL LLVM

https://reviews.llvm.org/D57052

Files:
  lib/Transforms/IPO/IPConstantPropagation.cpp
  test/Transforms/IPConstantProp/arg-count-mismatch.ll


Index: test/Transforms/IPConstantProp/arg-count-mismatch.ll
===================================================================
--- /dev/null
+++ test/Transforms/IPConstantProp/arg-count-mismatch.ll
@@ -0,0 +1,49 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -ipconstprop -S -o - | FileCheck %s
+
+; The original C source looked like this:
+;
+;   long long a101, b101, e101;
+;   volatile long c101;
+;   int d101;
+;
+;   static inline int bar(p1, p2)
+;   {
+;       return 0;
+;   }
+;
+;   void foo(unsigned p1)
+;   {
+;       long long *f = &b101, *g = &e101;
+;       c101 = 0;
+;       (void)((*f |= a101) - (*g = bar(d101)));
+;       c101 = (*f |= a101 &= p1) == d101;
+;   }
+;
+; When compiled with Clang it gives a warning
+;   warning: too few arguments in call to 'bar'
+;
+; This ll reproducer has been reduced to only include tha call.
+;
+; Note that -lint will report this as UB, but it passes -verify.
+
+; This test is just to verify that we do not crash/assert due to mismatch in
+; argument count between the caller and callee.
+
+define dso_local void @foo(i16 %a) {
+; CHECK-LABEL: @foo(
+; CHECK-NEXT:    [[CALL:%.*]] = call i16 bitcast (i16 (i16, i16)* @bar to i16 (i16)*)(i16 [[A:%.*]])
+; CHECK-NEXT:    ret void
+;
+  %call = call i16 bitcast (i16 (i16, i16) * @bar to i16 (i16) *)(i16 %a)
+  ret void
+}
+
+define internal i16 @bar(i16 %p1, i16 %p2) {
+; CHECK-LABEL: @bar(
+; CHECK-NEXT:    ret i16 0
+;
+  ret i16 0
+}
+
+
Index: lib/Transforms/IPO/IPConstantPropagation.cpp
===================================================================
--- lib/Transforms/IPO/IPConstantPropagation.cpp
+++ lib/Transforms/IPO/IPConstantPropagation.cpp
@@ -66,6 +66,12 @@
     if (!ACS)
       return false;
 
+    // Clang only gives warning for "too few arguments in call". Even if it is
+    // undefined behavior we should not crash? So simply bail out if argument
+    // count mismatches.
+    if (ACS.getNumArgOperands() != ArgumentConstants.size())
+      return false;
+
     // Check out all of the potentially constant arguments.  Note that we don't
     // inspect varargs here.
     Function::arg_iterator Arg = F.arg_begin();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57052.182903.patch
Type: text/x-patch
Size: 2211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190122/d6f2c0cc/attachment.bin>


More information about the llvm-commits mailing list