llvm.org GIT mirror llvm / 7005517
[WebAssembly] Support bitcasted function addresses with varargs. Generalize FixFunctionBitcasts to handle varargs functions. This in particular fixes the case where clang bitcasts away a varargs when calling a K&R-style function. This avoids interacting with tricky ABI details because it operates at the LLVM IR level before varargs ABI details are exposed. This fixes PR35385. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319186 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 2 years ago
5 changed file(s) with 58 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
10391039 for (const MCSymbol &S : Asm.symbols()) {
10401040 const auto &WS = static_cast(S);
10411041
1042 // Register types for all functions, including those with private linkage
1043 // (making them
1044 // because wasm always needs a type signature.
1045 if (WS.isFunction())
1046 registerFunctionType(WS);
1047
10421048 if (WS.isTemporary())
10431049 continue;
1044
1045 if (WS.isFunction())
1046 registerFunctionType(WS);
10471050
10481051 // If the symbol is not defined in this translation unit, import it.
10491052 if (!WS.isDefined(/*SetUsed=*/false)) {
106106 // Determine what arguments to pass.
107107 SmallVector Args;
108108 Function::arg_iterator AI = Wrapper->arg_begin();
109 Function::arg_iterator AE = Wrapper->arg_end();
109110 FunctionType::param_iterator PI = F->getFunctionType()->param_begin();
110111 FunctionType::param_iterator PE = F->getFunctionType()->param_end();
111 for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) {
112 for (; AI != AE && PI != PE; ++AI, ++PI) {
112113 if (AI->getType() != *PI) {
113114 Wrapper->eraseFromParent();
114115 return nullptr;
117118 }
118119 for (; PI != PE; ++PI)
119120 Args.push_back(UndefValue::get(*PI));
121 if (F->isVarArg())
122 for (; AI != AE; ++AI)
123 Args.push_back(&*AI);
120124
121125 CallInst *Call = CallInst::Create(F, Args, "", BB);
122126
157161 if (!Ty)
158162 continue;
159163
160 // Wasm varargs are not ABI-compatible with non-varargs. Just ignore
161 // such casts for now.
162 if (Ty->isVarArg() || F->isVarArg())
163 continue;
164
165164 auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr));
166165 if (Pair.second)
167166 Pair.first->second = CreateWrapper(F, Ty);
152152 ; CHECK-LABEL: call_constexpr:
153153 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 2{{$}}
154154 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 3{{$}}
155 ; CHECK-NEXT: call vararg_func@FUNCTION, $pop[[L0]], $pop[[L1]]{{$}}
155 ; CHECK-NEXT: call .Lbitcast@FUNCTION, $pop[[L0]], $pop[[L1]]{{$}}
156156 ; CHECK-NEXT: call other_void_nullary@FUNCTION{{$}}
157157 ; CHECK-NEXT: call void_nullary@FUNCTION{{$}}
158158 ; CHECK-NEXT: return{{$}}
0 ; RUN: llc < %s -asm-verbose=false | FileCheck %s
1
2 ; Test that function pointer casts casting away varargs are replaced with
3 ; wrappers.
4
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown-wasm"
7
8 define void @callWithArgs() {
9 entry:
10 call void bitcast (void (...)* @underspecified to void (i32, i32)*)(i32 0, i32 1)
11 call void(...) bitcast (void (i32, i32)* @specified to void (...)*)(i32 0, i32 1)
12 ret void
13 }
14
15 declare void @underspecified(...)
16 declare void @specified(i32, i32)
17
18 ; CHECK: callWithArgs:
19 ; CHECK: i32.const $push1=, 0
20 ; CHECK-NEXT: i32.const $push0=, 1
21 ; CHECK-NEXT: call .Lbitcast@FUNCTION, $pop1, $pop0
22 ; CHECK: call .Lbitcast.1@FUNCTION, $pop{{[0-9]+$}}
23
24 ; CHECK: .Lbitcast:
25 ; CHECK-NEXT: .param i32, i32{{$}}
26 ; CHECK: call underspecified@FUNCTION, $pop{{[0-9]+$}}
27
28 ; CHECK: .Lbitcast.1:
29 ; CHECK-NEXT: .param i32{{$}}
30 ; CHECK: call specified@FUNCTION, $pop{{[0-9]+}}, $pop{{[0-9]+$}}
1919 ; CHECK-NEXT: call .Lbitcast@FUNCTION{{$}}
2020 ; CHECK-NEXT: call .Lbitcast.1@FUNCTION{{$}}
2121 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0
22 ; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L0]]{{$}}
22 ; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L0]]{{$}}
2323 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0
24 ; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L1]]{{$}}
24 ; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L1]]{{$}}
2525 ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0
26 ; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L2]]{{$}}
26 ; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L2]]{{$}}
2727 ; CHECK-NEXT: call foo0@FUNCTION
28 ; CHECK-NEXT: i32.call $drop=, .Lbitcast.3@FUNCTION{{$}}
28 ; CHECK-NEXT: i32.call $drop=, .Lbitcast.5@FUNCTION{{$}}
2929 ; CHECK-NEXT: call foo2@FUNCTION{{$}}
3030 ; CHECK-NEXT: call foo1@FUNCTION{{$}}
3131 ; CHECK-NEXT: call foo3@FUNCTION{{$}}
5353 ; CHECK-LABEL: test_varargs:
5454 ; CHECK: set_global
5555 ; CHECK: i32.const $push[[L3:[0-9]+]]=, 0{{$}}
56 ; CHECK-NEXT: call vararg@FUNCTION, $pop[[L3]]{{$}}
56 ; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L3]]{{$}}
5757 ; CHECK-NEXT: i32.const $push[[L4:[0-9]+]]=, 0{{$}}
5858 ; CHECK-NEXT: i32.store 0($[[L5:[0-9]+]]), $pop[[L4]]{{$}}
59 ; CHECK-NEXT: call plain@FUNCTION, $[[L5]]{{$}}
59 ; CHECK-NEXT: call .Lbitcast.3@FUNCTION, $[[L5]]{{$}}
6060 define void @test_varargs() {
6161 call void bitcast (void (...)* @vararg to void (i32)*)(i32 0)
6262 call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0)
146146 ; CHECK-NEXT: end_function
147147
148148 ; CHECK-LABEL: .Lbitcast.2:
149 ; CHECK: call vararg@FUNCTION, $1{{$}}
150 ; CHECK: end_function
151
152 ; CHECK-LABEL: .Lbitcast.3:
153 ; CHECK: call plain@FUNCTION, $1{{$}}
154 ; CHECK: end_function
155
156 ; CHECK-LABEL: .Lbitcast.4:
149157 ; CHECK-NEXT: .param i32
150158 ; CHECK-NEXT: call foo0@FUNCTION{{$}}
151159 ; CHECK-NEXT: end_function
152160
153 ; CHECK-LABEL: .Lbitcast.3:
161 ; CHECK-LABEL: .Lbitcast.5:
154162 ; CHECK-NEXT: .result i32
155163 ; CHECK-NEXT: call foo1@FUNCTION{{$}}
156164 ; CHECK-NEXT: copy_local $push0=, $0