1//===-- WebAssemblyFixFunctionBitcasts.cpp - Fix function bitcasts --------===// 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 7//===----------------------------------------------------------------------===// 10/// Fix bitcasted functions. 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. 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. 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 23//===----------------------------------------------------------------------===// 35#define DEBUG_TYPE "wasm-fix-function-bitcasts" 38classFixFunctionBitcasts final :
publicModulePass {
40return"WebAssembly Fix Function Bitcasts";
54}
// End anonymous namespace 56char FixFunctionBitcasts::ID = 0;
58"Fix mismatching bitcasts for WebAssembly",
false,
false)
61returnnew FixFunctionBitcasts();
64// Recursively descend the def-use lists from V to find non-bitcast users of 68for (
User *U : V->users()) {
69if (
auto *BC = dyn_cast<BitCastOperator>(U))
71elseif (
auto *
A = dyn_cast<GlobalAlias>(U))
73elseif (
auto *CB = dyn_cast<CallBase>(U)) {
74Value *Callee = CB->getCalledOperand();
76// Skip calls where the function isn't the callee 78if (CB->getFunctionType() ==
F.getValueType())
79// Skip uses that are immediately called 81Uses.push_back(std::make_pair(CB, &
F));
86// Create a wrapper function with type Ty that calls F (which may have a 87// different type). Attempt to support common bitcasted function idioms: 88// - Call with more arguments than needed: arguments are dropped 89// - Call with fewer arguments than needed: arguments are filled in with poison 90// - Return value is not needed: drop it 91// - Return value needed but not present: supply a poison value 93// If the all the argument types of trivially castable to one another (i.e. 94// I32 vs pointer type) then we don't create a wrapper at all (return nullptr 97// If there is a type mismatch that we know would result in an invalid wasm 98// module then generate wrapper that contains unreachable (i.e. abort at 99// runtime). Such programs are deep into undefined behaviour territory, 100// but we choose to fail at runtime rather than generate and invalid module 101// or fail at compiler time. The reason we delay the error is that we want 102// to support the CMake which expects to be able to compile and link programs 103// that refer to functions with entirely incorrect signatures (this is how 104// CMake detects the existence of a function in a toolchain). 106// For bitcasts that involve struct types we don't know at this stage if they 107// would be equivalent at the wasm level and so we can't know if we need to 108// generate a wrapper. 113F->getName() +
"_bitcast", M);
114Wrapper->setAttributes(
F->getAttributes());
118// Determine what arguments to pass. 124bool TypeMismatch =
false;
125bool WrapperNeeded =
false;
127Type *ExpectedRtnType =
F->getFunctionType()->getReturnType();
128Type *RtnType = Ty->getReturnType();
130if ((
F->getFunctionType()->getNumParams() != Ty->getNumParams()) ||
131 (
F->getFunctionType()->isVarArg() != Ty->isVarArg()) ||
132 (ExpectedRtnType != RtnType))
135for (; AI != AE && PI != PE; ++AI, ++PI) {
137Type *ParamType = *PI;
139if (ArgType == ParamType) {
140 Args.push_back(&*AI);
146 Args.push_back(PtrCast);
148LLVM_DEBUG(
dbgs() <<
"createWrapper: struct param type in bitcast: " 149 <<
F->getName() <<
"\n");
150 WrapperNeeded =
false;
153 <<
F->getName() <<
"\n");
155 << *ParamType <<
" Got: " << *ArgType <<
"\n");
162if (WrapperNeeded && !TypeMismatch) {
163for (; PI != PE; ++PI)
166for (; AI != AE; ++AI)
167 Args.push_back(&*AI);
171Type *ExpectedRtnType =
F->getFunctionType()->getReturnType();
172Type *RtnType = Ty->getReturnType();
173// Determine what value to return. 176 }
elseif (ExpectedRtnType->
isVoidTy()) {
179 }
elseif (RtnType == ExpectedRtnType) {
188LLVM_DEBUG(
dbgs() <<
"createWrapper: struct return type in bitcast: " 189 <<
F->getName() <<
"\n");
190 WrapperNeeded =
false;
192LLVM_DEBUG(
dbgs() <<
"createWrapper: return type mismatch calling: " 193 <<
F->getName() <<
"\n");
195 <<
" Got: " << *RtnType <<
"\n");
201// Create a new wrapper that simply contains `unreachable`. 204F->getName() +
"_bitcast_invalid", M);
205Wrapper->setAttributes(
F->getAttributes());
208Wrapper->setName(
F->getName() +
"_bitcast_invalid");
209 }
elseif (!WrapperNeeded) {
219// Test whether a main function with type FuncTy should be rewritten to have 222// Only fix the main function if it's the standard zero-arg form. That way, 223// the standard cases will work as expected, and users will see signature 224// mismatches from the linker for non-standard cases. 225return FuncTy->getReturnType() == MainTy->getReturnType() &&
226 FuncTy->getNumParams() == 0 &&
230bool FixFunctionBitcasts::runOnModule(
Module &M) {
231LLVM_DEBUG(
dbgs() <<
"********** Fix Function Bitcasts **********\n");
237// Collect all the places that need wrappers. 239// Skip to fix when the function is swiftcc because swiftcc allows 240// bitcast type difference for swiftself and swifterror. 245// If we have a "main" function, and its type isn't 246// "int main(int argc, char *argv[])", create an artificial call with it 247// bitcasted to that type so that we generate a wrapper for it, so that 248// the C runtime can call it. 249if (
F.getName() ==
"main") {
257 << *
F.getFunctionType() <<
"\n");
261Uses.push_back(std::make_pair(CallMain, &
F));
268for (
auto &UseFunc :
Uses) {
273auto Pair = Wrappers.
insert(std::make_pair(std::make_pair(
F, Ty),
nullptr));
284// If we created a wrapper for main, rename the wrapper so that it's the 285// one that gets called from startup. 287 Main->
setName(
"__original_main");
292// The wrapper is not needed in this case as we don't need to export 296// Otherwise give the wrapper the same linkage as the original main 297// function, so that it can be called from the same places. 298 MainWrapper->setName(
"main");
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
static void findUses(Value *V, Function &F, SmallVectorImpl< std::pair< CallBase *, Function * > > &Uses)
static bool shouldFixMainFunction(FunctionType *FuncTy, FunctionType *MainTy)
static Function * createWrapper(Function *F, FunctionType *Ty)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
This class represents an incoming formal argument to a Function.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Value * getCalledOperand() const
FunctionType * getFunctionType() const
void setCalledOperand(Value *V)
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
A parsed version of the target data layout string in and methods for querying it.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Type::subtype_iterator param_iterator
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
VisibilityTypes getVisibility() const
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
This is an important class for using LLVM in a threaded context.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isStructTy() const
True if this is an instance of StructType.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
This function has undefined behavior.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void setName(const Twine &Name)
Change the name of the value.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ Swift
Calling convention for Swift.
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
ModulePass * createWebAssemblyFixFunctionBitcasts()