Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyLowerGlobalDtors.cpp - Lower @llvm.global_dtors --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// Lower @llvm.global_dtors. |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 11 | /// |
| 12 | /// WebAssembly doesn't have a builtin way to invoke static destructors. |
| 13 | /// Implement @llvm.global_dtors by creating wrapper functions that are |
| 14 | /// registered in @llvm.global_ctors and which contain a call to |
| 15 | /// `__cxa_atexit` to register their destructor functions. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "WebAssembly.h" |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/MapVector.h" |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/Intrinsics.h" |
| 24 | #include "llvm/IR/Module.h" |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | #define DEBUG_TYPE "wasm-lower-global-dtors" |
| 32 | |
| 33 | namespace { |
| 34 | class LowerGlobalDtors final : public ModulePass { |
| 35 | StringRef getPassName() const override { |
| 36 | return "WebAssembly Lower @llvm.global_dtors"; |
| 37 | } |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 40 | AU.setPreservesCFG(); |
| 41 | ModulePass::getAnalysisUsage(AU); |
| 42 | } |
| 43 | |
| 44 | bool runOnModule(Module &M) override; |
| 45 | |
| 46 | public: |
| 47 | static char ID; |
| 48 | LowerGlobalDtors() : ModulePass(ID) {} |
| 49 | }; |
| 50 | } // End anonymous namespace |
| 51 | |
| 52 | char LowerGlobalDtors::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS(LowerGlobalDtors, DEBUG_TYPE, |
| 54 | "Lower @llvm.global_dtors for WebAssembly", false, false) |
| 55 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 56 | ModulePass *llvm::createWebAssemblyLowerGlobalDtors() { |
| 57 | return new LowerGlobalDtors(); |
| 58 | } |
| 59 | |
| 60 | bool LowerGlobalDtors::runOnModule(Module &M) { |
Heejin Ahn | 569f090 | 2019-01-09 23:05:21 +0000 | [diff] [blame] | 61 | LLVM_DEBUG(dbgs() << "********** Lower Global Destructors **********\n"); |
| 62 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 63 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_dtors"); |
| 64 | if (!GV) |
| 65 | return false; |
| 66 | |
| 67 | const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 68 | if (!InitList) |
| 69 | return false; |
| 70 | |
| 71 | // Sanity-check @llvm.global_dtor's type. |
| 72 | StructType *ETy = dyn_cast<StructType>(InitList->getType()->getElementType()); |
| 73 | if (!ETy || ETy->getNumElements() != 3 || |
| 74 | !ETy->getTypeAtIndex(0U)->isIntegerTy() || |
| 75 | !ETy->getTypeAtIndex(1U)->isPointerTy() || |
| 76 | !ETy->getTypeAtIndex(2U)->isPointerTy()) |
| 77 | return false; // Not (int, ptr, ptr). |
| 78 | |
| 79 | // Collect the contents of @llvm.global_dtors, collated by priority and |
| 80 | // associated symbol. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 81 | std::map<uint16_t, MapVector<Constant *, std::vector<Constant *>>> DtorFuncs; |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 82 | for (Value *O : InitList->operands()) { |
| 83 | ConstantStruct *CS = dyn_cast<ConstantStruct>(O); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 84 | if (!CS) |
| 85 | continue; // Malformed. |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 86 | |
| 87 | ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0)); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 88 | if (!Priority) |
| 89 | continue; // Malformed. |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 90 | uint16_t PriorityValue = Priority->getLimitedValue(UINT16_MAX); |
| 91 | |
| 92 | Constant *DtorFunc = CS->getOperand(1); |
| 93 | if (DtorFunc->isNullValue()) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 94 | break; // Found a null terminator, skip the rest. |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 95 | |
| 96 | Constant *Associated = CS->getOperand(2); |
| 97 | Associated = cast<Constant>(Associated->stripPointerCastsNoFollowAliases()); |
| 98 | |
| 99 | DtorFuncs[PriorityValue][Associated].push_back(DtorFunc); |
| 100 | } |
| 101 | if (DtorFuncs.empty()) |
| 102 | return false; |
| 103 | |
| 104 | // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); |
| 105 | LLVMContext &C = M.getContext(); |
| 106 | PointerType *VoidStar = Type::getInt8PtrTy(C); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 107 | Type *AtExitFuncArgs[] = {VoidStar}; |
| 108 | FunctionType *AtExitFuncTy = |
| 109 | FunctionType::get(Type::getVoidTy(C), AtExitFuncArgs, |
| 110 | /*isVarArg=*/false); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 111 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 112 | Type *AtExitArgs[] = {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar}; |
| 113 | FunctionType *AtExitTy = FunctionType::get(Type::getInt32Ty(C), AtExitArgs, |
| 114 | /*isVarArg=*/false); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 115 | Constant *AtExit = M.getOrInsertFunction("__cxa_atexit", AtExitTy); |
| 116 | |
| 117 | // Declare __dso_local. |
| 118 | Constant *DsoHandle = M.getNamedValue("__dso_handle"); |
| 119 | if (!DsoHandle) { |
| 120 | Type *DsoHandleTy = Type::getInt8Ty(C); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 121 | GlobalVariable *Handle = new GlobalVariable( |
| 122 | M, DsoHandleTy, /*isConstant=*/true, |
| 123 | GlobalVariable::ExternalWeakLinkage, nullptr, "__dso_handle"); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 124 | Handle->setVisibility(GlobalVariable::HiddenVisibility); |
| 125 | DsoHandle = Handle; |
| 126 | } |
| 127 | |
| 128 | // For each unique priority level and associated symbol, generate a function |
| 129 | // to call all the destructors at that level, and a function to register the |
| 130 | // first function with __cxa_atexit. |
| 131 | for (auto &PriorityAndMore : DtorFuncs) { |
| 132 | uint16_t Priority = PriorityAndMore.first; |
| 133 | for (auto &AssociatedAndMore : PriorityAndMore.second) { |
| 134 | Constant *Associated = AssociatedAndMore.first; |
| 135 | |
| 136 | Function *CallDtors = Function::Create( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 137 | AtExitFuncTy, Function::PrivateLinkage, |
| 138 | "call_dtors" + |
| 139 | (Priority != UINT16_MAX ? (Twine(".") + Twine(Priority)) |
| 140 | : Twine()) + |
| 141 | (!Associated->isNullValue() ? (Twine(".") + Associated->getName()) |
| 142 | : Twine()), |
| 143 | &M); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 144 | BasicBlock *BB = BasicBlock::Create(C, "body", CallDtors); |
| 145 | |
| 146 | for (auto Dtor : AssociatedAndMore.second) |
| 147 | CallInst::Create(Dtor, "", BB); |
| 148 | ReturnInst::Create(C, BB); |
| 149 | |
| 150 | FunctionType *VoidVoid = FunctionType::get(Type::getVoidTy(C), |
| 151 | /*isVarArg=*/false); |
| 152 | Function *RegisterCallDtors = Function::Create( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 153 | VoidVoid, Function::PrivateLinkage, |
| 154 | "register_call_dtors" + |
| 155 | (Priority != UINT16_MAX ? (Twine(".") + Twine(Priority)) |
| 156 | : Twine()) + |
| 157 | (!Associated->isNullValue() ? (Twine(".") + Associated->getName()) |
| 158 | : Twine()), |
| 159 | &M); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 160 | BasicBlock *EntryBB = BasicBlock::Create(C, "entry", RegisterCallDtors); |
| 161 | BasicBlock *FailBB = BasicBlock::Create(C, "fail", RegisterCallDtors); |
| 162 | BasicBlock *RetBB = BasicBlock::Create(C, "return", RegisterCallDtors); |
| 163 | |
| 164 | Value *Null = ConstantPointerNull::get(VoidStar); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 165 | Value *Args[] = {CallDtors, Null, DsoHandle}; |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 166 | Value *Res = CallInst::Create(AtExit, Args, "call", EntryBB); |
| 167 | Value *Cmp = new ICmpInst(*EntryBB, ICmpInst::ICMP_NE, Res, |
| 168 | Constant::getNullValue(Res->getType())); |
| 169 | BranchInst::Create(FailBB, RetBB, Cmp, EntryBB); |
| 170 | |
| 171 | // If `__cxa_atexit` hits out-of-memory, trap, so that we don't misbehave. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 172 | // This should be very rare, because if the process is running out of |
| 173 | // memory before main has even started, something is wrong. |
| 174 | CallInst::Create(Intrinsic::getDeclaration(&M, Intrinsic::trap), "", |
| 175 | FailBB); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 176 | new UnreachableInst(C, FailBB); |
| 177 | |
| 178 | ReturnInst::Create(C, RetBB); |
| 179 | |
| 180 | // Now register the registration function with @llvm.global_ctors. |
| 181 | appendToGlobalCtors(M, RegisterCallDtors, Priority, Associated); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Now that we've lowered everything, remove @llvm.global_dtors. |
| 186 | GV->eraseFromParent(); |
| 187 | |
| 188 | return true; |
| 189 | } |