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 public: 52 static char ID; 53 FixFunctionBitcasts() : ModulePass(ID) {} 54 }; 55 } // End anonymous namespace 56 57 char FixFunctionBitcasts::ID = 0; 58 ModulePass *llvm::createWebAssemblyFixFunctionBitcasts() { 59 return new FixFunctionBitcasts(); 60 } 61 62 // Recursively descend the def-use lists from V to find non-bitcast users of 63 // bitcasts of V. 64 static void FindUses(Value *V, Function &F, 65 SmallVectorImpl<std::pair<Use *, Function *>> &Uses) { 66 for (Use &U : V->uses()) { 67 if (BitCastOperator *BC = dyn_cast<BitCastOperator>(U.getUser())) 68 FindUses(BC, F, Uses); 69 else if (U.get()->getType() != F.getType()) 70 Uses.push_back(std::make_pair(&U, &F)); 71 } 72 } 73 74 // Create a wrapper function with type Ty that calls F (which may have a 75 // different type). Attempt to support common bitcasted function idioms: 76 // - Call with more arguments than needed: arguments are dropped 77 // - Call with fewer arguments than needed: arguments are filled in with undef 78 // - Return value is not needed: drop it 79 // - Return value needed but not present: supply an undef 80 // 81 // For now, return nullptr without creating a wrapper if the wrapper cannot 82 // be generated due to incompatible types. 83 static Function *CreateWrapper(Function *F, FunctionType *Ty) { 84 Module *M = F->getParent(); 85 86 Function *Wrapper = 87 Function::Create(Ty, Function::PrivateLinkage, "bitcast", M); 88 BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper); 89 90 // Determine what arguments to pass. 91 SmallVector<Value *, 4> Args; 92 Function::arg_iterator AI = Wrapper->arg_begin(); 93 FunctionType::param_iterator PI = F->getFunctionType()->param_begin(); 94 FunctionType::param_iterator PE = F->getFunctionType()->param_end(); 95 for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) { 96 if (AI->getType() != *PI) { 97 Wrapper->eraseFromParent(); 98 return nullptr; 99 } 100 Args.push_back(&*AI); 101 } 102 for (; PI != PE; ++PI) 103 Args.push_back(UndefValue::get(*PI)); 104 105 CallInst *Call = CallInst::Create(F, Args, "", BB); 106 107 // Determine what value to return. 108 if (Ty->getReturnType()->isVoidTy()) 109 ReturnInst::Create(M->getContext(), BB); 110 else if (F->getFunctionType()->getReturnType()->isVoidTy()) 111 ReturnInst::Create(M->getContext(), UndefValue::get(Ty->getReturnType()), 112 BB); 113 else if (F->getFunctionType()->getReturnType() == Ty->getReturnType()) 114 ReturnInst::Create(M->getContext(), Call, BB); 115 else { 116 Wrapper->eraseFromParent(); 117 return nullptr; 118 } 119 120 return Wrapper; 121 } 122 123 bool FixFunctionBitcasts::runOnModule(Module &M) { 124 SmallVector<std::pair<Use *, Function *>, 0> Uses; 125 126 // Collect all the places that need wrappers. 127 for (Function &F : M) 128 FindUses(&F, F, Uses); 129 130 DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers; 131 132 for (auto &UseFunc : Uses) { 133 Use *U = UseFunc.first; 134 Function *F = UseFunc.second; 135 PointerType *PTy = cast<PointerType>(U->get()->getType()); 136 FunctionType *Ty = dyn_cast<FunctionType>(PTy->getElementType()); 137 138 // If the function is casted to something like i8* as a "generic pointer" 139 // to be later casted to something else, we can't generate a wrapper for it. 140 // Just ignore such casts for now. 141 if (!Ty) 142 continue; 143 144 auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr)); 145 if (Pair.second) 146 Pair.first->second = CreateWrapper(F, Ty); 147 148 Function *Wrapper = Pair.first->second; 149 if (!Wrapper) 150 continue; 151 152 if (isa<Constant>(U->get())) 153 U->get()->replaceAllUsesWith(Wrapper); 154 else 155 U->set(Wrapper); 156 } 157 158 return true; 159 } 160