]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / Target / WebAssembly / WebAssemblyFixFunctionBitcasts.cpp
1 //===-- WebAssemblyFixFunctionBitcasts.cpp - Fix function bitcasts --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Fix bitcasted functions.
11 ///
12 /// WebAssembly requires caller and callee signatures to match, however in LLVM,
13 /// some amount of slop is vaguely permitted. Detect mismatch by looking for
14 /// bitcasts of functions and rewrite them to use wrapper functions instead.
15 ///
16 /// This doesn't catch all cases, such as when a function's address is taken in
17 /// one place and casted in another, but it works for many common cases.
18 ///
19 /// Note that LLVM already optimizes away function bitcasts in common cases by
20 /// dropping arguments as needed, so this pass only ends up getting used in less
21 /// common cases.
22 ///
23 //===----------------------------------------------------------------------===//
24
25 #include "WebAssembly.h"
26 #include "llvm/IR/CallSite.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 INITIALIZE_PASS(FixFunctionBitcasts, DEBUG_TYPE,
59                 "Fix mismatching bitcasts for WebAssembly", false, false)
60
61 ModulePass *llvm::createWebAssemblyFixFunctionBitcasts() {
62   return new FixFunctionBitcasts();
63 }
64
65 // Recursively descend the def-use lists from V to find non-bitcast users of
66 // bitcasts of V.
67 static void findUses(Value *V, Function &F,
68                      SmallVectorImpl<std::pair<Use *, Function *>> &Uses,
69                      SmallPtrSetImpl<Constant *> &ConstantBCs) {
70   for (Use &U : V->uses()) {
71     if (auto *BC = dyn_cast<BitCastOperator>(U.getUser()))
72       findUses(BC, F, Uses, ConstantBCs);
73     else if (auto *A = dyn_cast<GlobalAlias>(U.getUser()))
74       findUses(A, F, Uses, ConstantBCs);
75     else if (U.get()->getType() != F.getType()) {
76       CallSite CS(U.getUser());
77       if (!CS)
78         // Skip uses that aren't immediately called
79         continue;
80       Value *Callee = CS.getCalledValue();
81       if (Callee != V)
82         // Skip calls where the function isn't the callee
83         continue;
84       if (isa<Constant>(U.get())) {
85         // Only add constant bitcasts to the list once; they get RAUW'd
86         auto C = ConstantBCs.insert(cast<Constant>(U.get()));
87         if (!C.second)
88           continue;
89       }
90       Uses.push_back(std::make_pair(&U, &F));
91     }
92   }
93 }
94
95 // Create a wrapper function with type Ty that calls F (which may have a
96 // different type). Attempt to support common bitcasted function idioms:
97 //  - Call with more arguments than needed: arguments are dropped
98 //  - Call with fewer arguments than needed: arguments are filled in with undef
99 //  - Return value is not needed: drop it
100 //  - Return value needed but not present: supply an undef
101 //
102 // If the all the argument types of trivially castable to one another (i.e.
103 // I32 vs pointer type) then we don't create a wrapper at all (return nullptr
104 // instead).
105 //
106 // If there is a type mismatch that we know would result in an invalid wasm
107 // module then generate wrapper that contains unreachable (i.e. abort at
108 // runtime).  Such programs are deep into undefined behaviour territory,
109 // but we choose to fail at runtime rather than generate and invalid module
110 // or fail at compiler time.  The reason we delay the error is that we want
111 // to support the CMake which expects to be able to compile and link programs
112 // that refer to functions with entirely incorrect signatures (this is how
113 // CMake detects the existence of a function in a toolchain).
114 //
115 // For bitcasts that involve struct types we don't know at this stage if they
116 // would be equivalent at the wasm level and so we can't know if we need to
117 // generate a wrapper.
118 static Function *createWrapper(Function *F, FunctionType *Ty) {
119   Module *M = F->getParent();
120
121   Function *Wrapper = Function::Create(Ty, Function::PrivateLinkage,
122                                        F->getName() + "_bitcast", M);
123   BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
124   const DataLayout &DL = BB->getModule()->getDataLayout();
125
126   // Determine what arguments to pass.
127   SmallVector<Value *, 4> Args;
128   Function::arg_iterator AI = Wrapper->arg_begin();
129   Function::arg_iterator AE = Wrapper->arg_end();
130   FunctionType::param_iterator PI = F->getFunctionType()->param_begin();
131   FunctionType::param_iterator PE = F->getFunctionType()->param_end();
132   bool TypeMismatch = false;
133   bool WrapperNeeded = false;
134
135   Type *ExpectedRtnType = F->getFunctionType()->getReturnType();
136   Type *RtnType = Ty->getReturnType();
137
138   if ((F->getFunctionType()->getNumParams() != Ty->getNumParams()) ||
139       (F->getFunctionType()->isVarArg() != Ty->isVarArg()) ||
140       (ExpectedRtnType != RtnType))
141     WrapperNeeded = true;
142
143   for (; AI != AE && PI != PE; ++AI, ++PI) {
144     Type *ArgType = AI->getType();
145     Type *ParamType = *PI;
146
147     if (ArgType == ParamType) {
148       Args.push_back(&*AI);
149     } else {
150       if (CastInst::isBitOrNoopPointerCastable(ArgType, ParamType, DL)) {
151         Instruction *PtrCast =
152             CastInst::CreateBitOrPointerCast(AI, ParamType, "cast");
153         BB->getInstList().push_back(PtrCast);
154         Args.push_back(PtrCast);
155       } else if (ArgType->isStructTy() || ParamType->isStructTy()) {
156         LLVM_DEBUG(dbgs() << "createWrapper: struct param type in bitcast: "
157                           << F->getName() << "\n");
158         WrapperNeeded = false;
159       } else {
160         LLVM_DEBUG(dbgs() << "createWrapper: arg type mismatch calling: "
161                           << F->getName() << "\n");
162         LLVM_DEBUG(dbgs() << "Arg[" << Args.size() << "] Expected: "
163                           << *ParamType << " Got: " << *ArgType << "\n");
164         TypeMismatch = true;
165         break;
166       }
167     }
168   }
169
170   if (WrapperNeeded && !TypeMismatch) {
171     for (; PI != PE; ++PI)
172       Args.push_back(UndefValue::get(*PI));
173     if (F->isVarArg())
174       for (; AI != AE; ++AI)
175         Args.push_back(&*AI);
176
177     CallInst *Call = CallInst::Create(F, Args, "", BB);
178
179     Type *ExpectedRtnType = F->getFunctionType()->getReturnType();
180     Type *RtnType = Ty->getReturnType();
181     // Determine what value to return.
182     if (RtnType->isVoidTy()) {
183       ReturnInst::Create(M->getContext(), BB);
184     } else if (ExpectedRtnType->isVoidTy()) {
185       LLVM_DEBUG(dbgs() << "Creating dummy return: " << *RtnType << "\n");
186       ReturnInst::Create(M->getContext(), UndefValue::get(RtnType), BB);
187     } else if (RtnType == ExpectedRtnType) {
188       ReturnInst::Create(M->getContext(), Call, BB);
189     } else if (CastInst::isBitOrNoopPointerCastable(ExpectedRtnType, RtnType,
190                                                     DL)) {
191       Instruction *Cast =
192           CastInst::CreateBitOrPointerCast(Call, RtnType, "cast");
193       BB->getInstList().push_back(Cast);
194       ReturnInst::Create(M->getContext(), Cast, BB);
195     } else if (RtnType->isStructTy() || ExpectedRtnType->isStructTy()) {
196       LLVM_DEBUG(dbgs() << "createWrapper: struct return type in bitcast: "
197                         << F->getName() << "\n");
198       WrapperNeeded = false;
199     } else {
200       LLVM_DEBUG(dbgs() << "createWrapper: return type mismatch calling: "
201                         << F->getName() << "\n");
202       LLVM_DEBUG(dbgs() << "Expected: " << *ExpectedRtnType
203                         << " Got: " << *RtnType << "\n");
204       TypeMismatch = true;
205     }
206   }
207
208   if (TypeMismatch) {
209     // Create a new wrapper that simply contains `unreachable`.
210     Wrapper->eraseFromParent();
211     Wrapper = Function::Create(Ty, Function::PrivateLinkage,
212                                F->getName() + "_bitcast_invalid", M);
213     BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
214     new UnreachableInst(M->getContext(), BB);
215     Wrapper->setName(F->getName() + "_bitcast_invalid");
216   } else if (!WrapperNeeded) {
217     LLVM_DEBUG(dbgs() << "createWrapper: no wrapper needed: " << F->getName()
218                       << "\n");
219     Wrapper->eraseFromParent();
220     return nullptr;
221   }
222   LLVM_DEBUG(dbgs() << "createWrapper: " << F->getName() << "\n");
223   return Wrapper;
224 }
225
226 // Test whether a main function with type FuncTy should be rewritten to have
227 // type MainTy.
228 static bool shouldFixMainFunction(FunctionType *FuncTy, FunctionType *MainTy) {
229   // Only fix the main function if it's the standard zero-arg form. That way,
230   // the standard cases will work as expected, and users will see signature
231   // mismatches from the linker for non-standard cases.
232   return FuncTy->getReturnType() == MainTy->getReturnType() &&
233          FuncTy->getNumParams() == 0 &&
234          !FuncTy->isVarArg();
235 }
236
237 bool FixFunctionBitcasts::runOnModule(Module &M) {
238   LLVM_DEBUG(dbgs() << "********** Fix Function Bitcasts **********\n");
239
240   Function *Main = nullptr;
241   CallInst *CallMain = nullptr;
242   SmallVector<std::pair<Use *, Function *>, 0> Uses;
243   SmallPtrSet<Constant *, 2> ConstantBCs;
244
245   // Collect all the places that need wrappers.
246   for (Function &F : M) {
247     findUses(&F, F, Uses, ConstantBCs);
248
249     // If we have a "main" function, and its type isn't
250     // "int main(int argc, char *argv[])", create an artificial call with it
251     // bitcasted to that type so that we generate a wrapper for it, so that
252     // the C runtime can call it.
253     if (F.getName() == "main") {
254       Main = &F;
255       LLVMContext &C = M.getContext();
256       Type *MainArgTys[] = {Type::getInt32Ty(C),
257                             PointerType::get(Type::getInt8PtrTy(C), 0)};
258       FunctionType *MainTy = FunctionType::get(Type::getInt32Ty(C), MainArgTys,
259                                                /*isVarArg=*/false);
260       if (shouldFixMainFunction(F.getFunctionType(), MainTy)) {
261         LLVM_DEBUG(dbgs() << "Found `main` function with incorrect type: "
262                           << *F.getFunctionType() << "\n");
263         Value *Args[] = {UndefValue::get(MainArgTys[0]),
264                          UndefValue::get(MainArgTys[1])};
265         Value *Casted =
266             ConstantExpr::getBitCast(Main, PointerType::get(MainTy, 0));
267         CallMain = CallInst::Create(MainTy, Casted, Args, "call_main");
268         Use *UseMain = &CallMain->getOperandUse(2);
269         Uses.push_back(std::make_pair(UseMain, &F));
270       }
271     }
272   }
273
274   DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers;
275
276   for (auto &UseFunc : Uses) {
277     Use *U = UseFunc.first;
278     Function *F = UseFunc.second;
279     auto *PTy = cast<PointerType>(U->get()->getType());
280     auto *Ty = dyn_cast<FunctionType>(PTy->getElementType());
281
282     // If the function is casted to something like i8* as a "generic pointer"
283     // to be later casted to something else, we can't generate a wrapper for it.
284     // Just ignore such casts for now.
285     if (!Ty)
286       continue;
287
288     auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr));
289     if (Pair.second)
290       Pair.first->second = createWrapper(F, Ty);
291
292     Function *Wrapper = Pair.first->second;
293     if (!Wrapper)
294       continue;
295
296     if (isa<Constant>(U->get()))
297       U->get()->replaceAllUsesWith(Wrapper);
298     else
299       U->set(Wrapper);
300   }
301
302   // If we created a wrapper for main, rename the wrapper so that it's the
303   // one that gets called from startup.
304   if (CallMain) {
305     Main->setName("__original_main");
306     auto *MainWrapper =
307         cast<Function>(CallMain->getCalledValue()->stripPointerCasts());
308     delete CallMain;
309     if (Main->isDeclaration()) {
310       // The wrapper is not needed in this case as we don't need to export
311       // it to anyone else.
312       MainWrapper->eraseFromParent();
313     } else {
314       // Otherwise give the wrapper the same linkage as the original main
315       // function, so that it can be called from the same places.
316       MainWrapper->setName("main");
317       MainWrapper->setLinkage(Main->getLinkage());
318       MainWrapper->setVisibility(Main->getVisibility());
319     }
320   }
321
322   return true;
323 }