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 } 149