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