From 2803bfaf001241a98608c263a824a5f5ec542511 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 28 Nov 2017 17:15:03 +0000 Subject: [PATCH] [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. llvm-svn: 319186 --- llvm/lib/MC/WasmObjectWriter.cpp | 9 ++++--- .../WebAssembly/WebAssemblyFixFunctionBitcasts.cpp | 11 ++++---- llvm/test/CodeGen/WebAssembly/call.ll | 2 +- .../WebAssembly/function-bitcasts-varargs.ll | 31 ++++++++++++++++++++++ llvm/test/CodeGen/WebAssembly/function-bitcasts.ll | 22 ++++++++++----- 5 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 llvm/test/CodeGen/WebAssembly/function-bitcasts-varargs.ll diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index 2297084..6e9088b 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -1040,12 +1040,15 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, for (const MCSymbol &S : Asm.symbols()) { const auto &WS = static_cast(S); - if (WS.isTemporary()) - continue; - + // Register types for all functions, including those with private linkage + // (making them + // because wasm always needs a type signature. if (WS.isFunction()) registerFunctionType(WS); + if (WS.isTemporary()) + continue; + // If the symbol is not defined in this translation unit, import it. if (!WS.isDefined(/*SetUsed=*/false)) { WasmImport Import; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp index 19df75c..6473a2b 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp @@ -107,9 +107,10 @@ static Function *CreateWrapper(Function *F, FunctionType *Ty) { // Determine what arguments to pass. SmallVector Args; Function::arg_iterator AI = Wrapper->arg_begin(); + Function::arg_iterator AE = Wrapper->arg_end(); FunctionType::param_iterator PI = F->getFunctionType()->param_begin(); FunctionType::param_iterator PE = F->getFunctionType()->param_end(); - for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) { + for (; AI != AE && PI != PE; ++AI, ++PI) { if (AI->getType() != *PI) { Wrapper->eraseFromParent(); return nullptr; @@ -118,6 +119,9 @@ static Function *CreateWrapper(Function *F, FunctionType *Ty) { } for (; PI != PE; ++PI) Args.push_back(UndefValue::get(*PI)); + if (F->isVarArg()) + for (; AI != AE; ++AI) + Args.push_back(&*AI); CallInst *Call = CallInst::Create(F, Args, "", BB); @@ -158,11 +162,6 @@ bool FixFunctionBitcasts::runOnModule(Module &M) { if (!Ty) continue; - // Wasm varargs are not ABI-compatible with non-varargs. Just ignore - // such casts for now. - if (Ty->isVarArg() || F->isVarArg()) - continue; - auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr)); if (Pair.second) Pair.first->second = CreateWrapper(F, Ty); diff --git a/llvm/test/CodeGen/WebAssembly/call.ll b/llvm/test/CodeGen/WebAssembly/call.ll index dfa0cf5..74c9fb6 100644 --- a/llvm/test/CodeGen/WebAssembly/call.ll +++ b/llvm/test/CodeGen/WebAssembly/call.ll @@ -153,7 +153,7 @@ define void @coldcc_tail_call_void_nullary() { ; CHECK-LABEL: call_constexpr: ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 2{{$}} ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 3{{$}} -; CHECK-NEXT: call vararg_func@FUNCTION, $pop[[L0]], $pop[[L1]]{{$}} +; CHECK-NEXT: call .Lbitcast@FUNCTION, $pop[[L0]], $pop[[L1]]{{$}} ; CHECK-NEXT: call other_void_nullary@FUNCTION{{$}} ; CHECK-NEXT: call void_nullary@FUNCTION{{$}} ; CHECK-NEXT: return{{$}} diff --git a/llvm/test/CodeGen/WebAssembly/function-bitcasts-varargs.ll b/llvm/test/CodeGen/WebAssembly/function-bitcasts-varargs.ll new file mode 100644 index 0000000..c08fe8f --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/function-bitcasts-varargs.ll @@ -0,0 +1,31 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that function pointer casts casting away varargs are replaced with +; wrappers. + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown-wasm" + +define void @callWithArgs() { +entry: + call void bitcast (void (...)* @underspecified to void (i32, i32)*)(i32 0, i32 1) + call void(...) bitcast (void (i32, i32)* @specified to void (...)*)(i32 0, i32 1) + ret void +} + +declare void @underspecified(...) +declare void @specified(i32, i32) + +; CHECK: callWithArgs: +; CHECK: i32.const $push1=, 0 +; CHECK-NEXT: i32.const $push0=, 1 +; CHECK-NEXT: call .Lbitcast@FUNCTION, $pop1, $pop0 +; CHECK: call .Lbitcast.1@FUNCTION, $pop{{[0-9]+$}} + +; CHECK: .Lbitcast: +; CHECK-NEXT: .param i32, i32{{$}} +; CHECK: call underspecified@FUNCTION, $pop{{[0-9]+$}} + +; CHECK: .Lbitcast.1: +; CHECK-NEXT: .param i32{{$}} +; CHECK: call specified@FUNCTION, $pop{{[0-9]+}}, $pop{{[0-9]+$}} diff --git a/llvm/test/CodeGen/WebAssembly/function-bitcasts.ll b/llvm/test/CodeGen/WebAssembly/function-bitcasts.ll index 3e796e3..17843a2 100644 --- a/llvm/test/CodeGen/WebAssembly/function-bitcasts.ll +++ b/llvm/test/CodeGen/WebAssembly/function-bitcasts.ll @@ -20,13 +20,13 @@ declare void @foo3() ; CHECK-NEXT: call .Lbitcast@FUNCTION{{$}} ; CHECK-NEXT: call .Lbitcast.1@FUNCTION{{$}} ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0 -; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L0]]{{$}} +; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L0]]{{$}} ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0 -; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L1]]{{$}} +; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L1]]{{$}} ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0 -; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L2]]{{$}} +; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L2]]{{$}} ; CHECK-NEXT: call foo0@FUNCTION -; CHECK-NEXT: i32.call $drop=, .Lbitcast.3@FUNCTION{{$}} +; CHECK-NEXT: i32.call $drop=, .Lbitcast.5@FUNCTION{{$}} ; CHECK-NEXT: call foo2@FUNCTION{{$}} ; CHECK-NEXT: call foo1@FUNCTION{{$}} ; CHECK-NEXT: call foo3@FUNCTION{{$}} @@ -54,10 +54,10 @@ entry: ; CHECK-LABEL: test_varargs: ; CHECK: set_global ; CHECK: i32.const $push[[L3:[0-9]+]]=, 0{{$}} -; CHECK-NEXT: call vararg@FUNCTION, $pop[[L3]]{{$}} +; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L3]]{{$}} ; CHECK-NEXT: i32.const $push[[L4:[0-9]+]]=, 0{{$}} ; CHECK-NEXT: i32.store 0($[[L5:[0-9]+]]), $pop[[L4]]{{$}} -; CHECK-NEXT: call plain@FUNCTION, $[[L5]]{{$}} +; CHECK-NEXT: call .Lbitcast.3@FUNCTION, $[[L5]]{{$}} define void @test_varargs() { call void bitcast (void (...)* @vararg to void (i32)*)(i32 0) call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0) @@ -147,11 +147,19 @@ end: ; CHECK-NEXT: end_function ; CHECK-LABEL: .Lbitcast.2: +; CHECK: call vararg@FUNCTION, $1{{$}} +; CHECK: end_function + +; CHECK-LABEL: .Lbitcast.3: +; CHECK: call plain@FUNCTION, $1{{$}} +; CHECK: end_function + +; CHECK-LABEL: .Lbitcast.4: ; CHECK-NEXT: .param i32 ; CHECK-NEXT: call foo0@FUNCTION{{$}} ; CHECK-NEXT: end_function -; CHECK-LABEL: .Lbitcast.3: +; CHECK-LABEL: .Lbitcast.5: ; CHECK-NEXT: .result i32 ; CHECK-NEXT: call foo1@FUNCTION{{$}} ; CHECK-NEXT: copy_local $push0=, $0 -- 2.7.4