Skip to content

Commit 1b63745

Browse files
author
Dan Gohman
committed
[WebAssembly] Add a pass to create wrappers for function bitcasts.
WebAssembly requires caller and callee signatures to match exactly. In LLVM, there are a variety of circumstances where signatures may be mismatched in practice, and one can bitcast a function address to another type to call it as that type. This patch adds a pass which replaces bitcasted function addresses with wrappers to replace the bitcasts. This doesn't catch everything, but it does match many common cases. llvm-svn: 291315
1 parent 1d35775 commit 1b63745

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

llvm/lib/Target/WebAssembly/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_llvm_target(WebAssemblyCodeGen
1717
WebAssemblyExplicitLocals.cpp
1818
WebAssemblyFastISel.cpp
1919
WebAssemblyFixIrreducibleControlFlow.cpp
20+
WebAssemblyFixFunctionBitcasts.cpp
2021
WebAssemblyFrameLowering.cpp
2122
WebAssemblyISelDAGToDAG.cpp
2223
WebAssemblyISelLowering.cpp

llvm/lib/Target/WebAssembly/WebAssembly.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class FunctionPass;
2828
// LLVM IR passes.
2929
ModulePass *createWebAssemblyLowerEmscriptenEHSjLj(bool DoEH, bool DoSjLj);
3030
void initializeWebAssemblyLowerEmscriptenEHSjLjPass(PassRegistry &);
31+
ModulePass *createWebAssemblyFixFunctionBitcasts();
3132
FunctionPass *createWebAssemblyOptimizeReturned();
3233

3334
// ISel and immediate followup passes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//===-- WebAssemblyFixFunctionBitcasts.cpp - Fix function bitcasts --------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// \brief Fix bitcasted functions.
12+
///
13+
/// WebAssembly requires caller and callee signatures to match, however in LLVM,
14+
/// some amount of slop is vaguely permitted. Detect mismatch by looking for
15+
/// bitcasts of functions and rewrite them to use wrapper functions instead.
16+
///
17+
/// This doesn't catch all cases, such as when a function's address is taken in
18+
/// one place and casted in another, but it works for many common cases.
19+
///
20+
/// Note that LLVM already optimizes away function bitcasts in common cases by
21+
/// dropping arguments as needed, so this pass only ends up getting used in less
22+
/// common cases.
23+
///
24+
//===----------------------------------------------------------------------===//
25+
26+
#include "WebAssembly.h"
27+
#include "llvm/IR/Constants.h"
28+
#include "llvm/IR/Instructions.h"
29+
#include "llvm/IR/Module.h"
30+
#include "llvm/IR/Operator.h"
31+
#include "llvm/Pass.h"
32+
#include "llvm/Support/Debug.h"
33+
#include "llvm/Support/raw_ostream.h"
34+
using namespace llvm;
35+
36+
#define DEBUG_TYPE "wasm-fix-function-bitcasts"
37+
38+
namespace {
39+
class FixFunctionBitcasts final : public ModulePass {
40+
StringRef getPassName() const override {
41+
return "WebAssembly Fix Function Bitcasts";
42+
}
43+
44+
void getAnalysisUsage(AnalysisUsage &AU) const override {
45+
AU.setPreservesCFG();
46+
ModulePass::getAnalysisUsage(AU);
47+
}
48+
49+
bool runOnModule(Module &M) override;
50+
51+
SmallVector<std::pair<Use *, Function *>, 0> Uses;
52+
53+
public:
54+
static char ID;
55+
FixFunctionBitcasts() : ModulePass(ID) {}
56+
};
57+
} // End anonymous namespace
58+
59+
char FixFunctionBitcasts::ID = 0;
60+
ModulePass *llvm::createWebAssemblyFixFunctionBitcasts() {
61+
return new FixFunctionBitcasts();
62+
}
63+
64+
// Recursively descend the def-use lists from V to find non-bitcast users of
65+
// bitcasts of V.
66+
static void FindUses(Value *V, Function &F,
67+
SmallVectorImpl<std::pair<Use *, Function *>> &Uses) {
68+
for (Use &U : V->uses()) {
69+
if (BitCastOperator *BC = dyn_cast<BitCastOperator>(U.getUser()))
70+
FindUses(BC, F, Uses);
71+
else if (U.get()->getType() != F.getType())
72+
Uses.push_back(std::make_pair(&U, &F));
73+
}
74+
}
75+
76+
// Create a wrapper function with type Ty that calls F (which may have a
77+
// different type). Attempt to support common bitcasted function idioms:
78+
// - Call with more arguments than needed: arguments are dropped
79+
// - Call with fewer arguments than needed: arguments are filled in with undef
80+
// - Return value is not needed: drop it
81+
// - Return value needed but not present: supply an undef
82+
static Function *CreateWrapper(Function *F, FunctionType *Ty) {
83+
Module *M = F->getParent();
84+
85+
Function *Wrapper =
86+
Function::Create(Ty, Function::PrivateLinkage, "bitcast", M);
87+
BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
88+
89+
// Determine what arguments to pass.
90+
SmallVector<Value *, 4> Args;
91+
Function::arg_iterator AI = Wrapper->arg_begin();
92+
FunctionType::param_iterator PI = F->getFunctionType()->param_begin();
93+
FunctionType::param_iterator PE = F->getFunctionType()->param_end();
94+
for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) {
95+
assert(AI->getType() == *PI &&
96+
"mismatched argument types not supported yet");
97+
Args.push_back(&*AI);
98+
}
99+
for (; PI != PE; ++PI)
100+
Args.push_back(UndefValue::get(*PI));
101+
102+
CallInst *Call = CallInst::Create(F, Args, "", BB);
103+
104+
// Determine what value to return.
105+
if (Ty->getReturnType()->isVoidTy())
106+
ReturnInst::Create(M->getContext(), BB);
107+
else if (F->getFunctionType()->getReturnType()->isVoidTy())
108+
ReturnInst::Create(M->getContext(), UndefValue::get(Ty->getReturnType()),
109+
BB);
110+
else if (F->getFunctionType()->getReturnType() == Ty->getReturnType())
111+
ReturnInst::Create(M->getContext(), Call, BB);
112+
else
113+
llvm_unreachable("mismatched return types not supported yet");
114+
115+
return Wrapper;
116+
}
117+
118+
bool FixFunctionBitcasts::runOnModule(Module &M) {
119+
// Collect all the places that need wrappers.
120+
for (Function &F : M)
121+
FindUses(&F, F, Uses);
122+
123+
DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers;
124+
125+
for (auto &UseFunc : Uses) {
126+
Use *U = UseFunc.first;
127+
Function *F = UseFunc.second;
128+
PointerType *PTy = cast<PointerType>(U->get()->getType());
129+
FunctionType *Ty = dyn_cast<FunctionType>(PTy->getElementType());
130+
131+
// If the function is casted to something like i8* as a "generic pointer"
132+
// to be later casted to something else, we can't generate a wrapper for it.
133+
// Just ignore such casts for now.
134+
if (!Ty)
135+
continue;
136+
137+
auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr));
138+
if (Pair.second)
139+
Pair.first->second = CreateWrapper(F, Ty);
140+
141+
if (isa<Constant>(U->get()))
142+
U->get()->replaceAllUsesWith(Pair.first->second);
143+
else
144+
U->set(Pair.first->second);
145+
}
146+
147+
return true;
148+
}

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ void WebAssemblyPassConfig::addIRPasses() {
163163
// control specifically what gets lowered.
164164
addPass(createAtomicExpandPass(TM));
165165

166+
// Fix function bitcasts, as WebAssembly requires caller and callee signatures
167+
// to match.
168+
addPass(createWebAssemblyFixFunctionBitcasts());
169+
166170
// Optimize "returned" function attributes.
167171
if (getOptLevel() != CodeGenOpt::None)
168172
addPass(createWebAssemblyOptimizeReturned());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; RUN: llc < %s -asm-verbose=false | FileCheck %s
2+
3+
; Test that function pointer casts are replaced with wrappers.
4+
5+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6+
target triple = "wasm32-unknown-unknown"
7+
8+
; CHECK-LABEL: test:
9+
; CHECK-NEXT: call .Lbitcast@FUNCTION{{$}}
10+
; CHECK-NEXT: call .Lbitcast.1@FUNCTION{{$}}
11+
; CHECK-NEXT: i32.const $push[[L0:[0-9]*]]=, 0
12+
; CHECK-NEXT: call .Lbitcast.2@FUNCTION, $pop[[L0]]{{$}}
13+
; CHECK-NEXT: i32.call $drop=, .Lbitcast.3@FUNCTION{{$}}
14+
; CHECK-NEXT: call foo2@FUNCTION{{$}}
15+
; CHECK-NEXT: call foo3@FUNCTION{{$}}
16+
; CHECK-NEXT: .endfunc
17+
18+
; CHECK-LABEL: .Lbitcast:
19+
; CHECK-NEXT: .local i32
20+
; CHECK-NEXT: call has_i32_arg@FUNCTION, $0{{$}}
21+
; CHECK-NEXT: .endfunc
22+
23+
; CHECK-LABEL: .Lbitcast.1:
24+
; CHECK-NEXT: call $drop=, has_i32_ret@FUNCTION{{$}}
25+
; CHECK-NEXT: .endfunc
26+
27+
; CHECK-LABEL: .Lbitcast.2:
28+
; CHECK-NEXT: .param i32
29+
; CHECK-NEXT: call foo0@FUNCTION{{$}}
30+
; CHECK-NEXT: .endfunc
31+
32+
; CHECK-LABEL: .Lbitcast.3:
33+
; CHECK-NEXT: .result i32
34+
; CHECK-NEXT: .local i32
35+
; CHECK-NEXT: call foo1@FUNCTION{{$}}
36+
; CHECK-NEXT: copy_local $push0=, $0
37+
; CHECK-NEXT: .endfunc
38+
39+
declare void @has_i32_arg(i32)
40+
declare i32 @has_i32_ret()
41+
42+
declare void @foo0()
43+
declare void @foo1()
44+
declare void @foo2()
45+
declare void @foo3()
46+
47+
define void @test() {
48+
entry:
49+
call void bitcast (void (i32)* @has_i32_arg to void ()*)()
50+
call void bitcast (i32 ()* @has_i32_ret to void ()*)()
51+
call void bitcast (void ()* @foo0 to void (i32)*)(i32 0)
52+
%t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
53+
call void bitcast (void ()* @foo2 to void ()*)()
54+
call void @foo3()
55+
ret void
56+
}

0 commit comments

Comments
 (0)