llvm.org GIT mirror llvm / d0b3c51
[IPCP] Don't crash due to arg count/type mismatch between caller/callee Summary: This patch avoids an assert in IPConstantPropagation when there is a argument count/type 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 or Value::doRAUW) seems like a simple solution. The problem is exposed by the fact that AbstractCallSites will look through a bitcast at the callee position of a call/invoke. Reviewers: jdoerfert, reames, efriedma Reviewed By: jdoerfert, efriedma Subscribers: eli.friedman, efriedma, llvm-commits Differential Revision: https://reviews.llvm.org/D57052 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352469 91177308-0d34-0410-b5e6-96231b3b80d8 Bjorn Pettersson 7 months ago
3 changed file(s) with 107 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
6565 if (!ACS)
6666 return false;
6767
68 // Mismatched argument count is undefined behavior. Simply bail out to avoid
69 // handling of such situations below (avoiding asserts/crashes).
70 unsigned NumActualArgs = ACS.getNumArgOperands();
71 if (F.isVarArg() ? ArgumentConstants.size() > NumActualArgs
72 : ArgumentConstants.size() != NumActualArgs)
73 return false;
74
6875 // Check out all of the potentially constant arguments. Note that we don't
6976 // inspect varargs here.
7077 Function::arg_iterator Arg = F.arg_begin();
7683
7784 Value *V = ACS.getCallArgOperand(i);
7885 Constant *C = dyn_cast_or_null(V);
86
87 // Mismatched argument type is undefined behavior. Simply bail out to avoid
88 // handling of such situations below (avoiding asserts/crashes).
89 if (C && Arg->getType() != C->getType())
90 return false;
7991
8092 // We can only propagate thread independent values through callbacks.
8193 // This is different to direct/indirect call sites because for them we
0 ; RUN: opt < %s -ipconstprop -S -o - | FileCheck %s
1
2 ; The original C source looked like this:
3 ;
4 ; long long a101, b101, e101;
5 ; volatile long c101;
6 ; int d101;
7 ;
8 ; static inline int bar(p1, p2)
9 ; {
10 ; return 0;
11 ; }
12 ;
13 ; void foo(unsigned p1)
14 ; {
15 ; long long *f = &b101, *g = &e101;
16 ; c101 = 0;
17 ; (void)((*f |= a101) - (*g = bar(d101)));
18 ; c101 = (*f |= a101 &= p1) == d101;
19 ; }
20 ;
21 ; When compiled with Clang it gives a warning
22 ; warning: too few arguments in call to 'bar'
23 ;
24 ; This ll reproducer has been reduced to only include tha call.
25 ;
26 ; Note that -lint will report this as UB, but it passes -verify.
27
28 ; This test is just to verify that we do not crash/assert due to mismatch in
29 ; argument count between the caller and callee.
30
31 define dso_local void @foo(i16 %a) {
32 ; CHECK-LABEL: @foo(
33 ; CHECK-NEXT: [[CALL:%.*]] = call i16 bitcast (i16 (i16, i16)* @bar to i16 (i16)*)(i16 [[A:%.*]])
34 ; CHECK-NEXT: ret void
35 ;
36 %call = call i16 bitcast (i16 (i16, i16) * @bar to i16 (i16) *)(i16 %a)
37 ret void
38 }
39
40 define internal i16 @bar(i16 %p1, i16 %p2) {
41 ; CHECK-LABEL: @bar(
42 ; CHECK-NEXT: ret i16 0
43 ;
44 ret i16 0
45 }
46
47 ;-------------------------------------------------------------------------------
48 ; Additional tests to verify that we still optimize when having a mismatch
49 ; in argument count due to varargs (as long as all non-variadic arguments have
50 ; been provided),
51
52 define dso_local void @vararg_tests(i16 %a) {
53 %call1 = call i16 (i16, ...) @vararg_prop(i16 7, i16 8, i16 %a)
54 %call2 = call i16 bitcast (i16 (i16, i16, ...) * @vararg_no_prop to i16 (i16) *) (i16 7)
55 ret void
56 }
57
58 define internal i16 @vararg_prop(i16 %p1, ...) {
59 ; CHECK-LABEL: define internal i16 @vararg_prop(
60 ; CHECK-NEXT: ret i16 7
61 ;
62 ret i16 %p1
63 }
64
65 define internal i16 @vararg_no_prop(i16 %p1, i16 %p2, ...) {
66 ; CHECK-LABEL: define internal i16 @vararg_no_prop(
67 ; CHECK-NEXT: ret i16 [[P1:%.*]]
68 ;
69 ret i16 %p1
70 }
71
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt < %s -ipconstprop -S -o - | FileCheck %s
2
3 ; This test is just to verify that we do not crash/assert due to mismatch in
4 ; argument type between the caller and callee.
5
6 define dso_local void @foo(i16 %a) {
7 ; CHECK-LABEL: @foo(
8 ; CHECK-NEXT: [[CALL:%.*]] = call i16 bitcast (i16 (i16, i16)* @bar to i16 (i16, i32)*)(i16 [[A:%.*]], i32 7)
9 ; CHECK-NEXT: ret void
10 ;
11 %call = call i16 bitcast (i16 (i16, i16) * @bar to i16 (i16, i32) *)(i16 %a, i32 7)
12 ret void
13 }
14
15 define internal i16 @bar(i16 %p1, i16 %p2) {
16 ; CHECK-LABEL: @bar(
17 ; CHECK-NEXT: ret i16 [[P2:%.*]]
18 ;
19 ret i16 %p2
20 }
21
22