1 //===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===//
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 // This transformation is designed for use by code generators which use
10 // WebAssembly exception handling scheme. This currently supports C++
11 // exceptions.
12 //
13 // WebAssembly exception handling uses Windows exception IR for the middle level
14 // representation. This pass does the following transformation for every
15 // catchpad block:
16 // (In C-style pseudocode)
17 //
18 // - Before:
19 // catchpad ...
20 // exn = wasm.get.exception();
21 // selector = wasm.get.selector();
22 // ...
23 //
24 // - After:
25 // catchpad ...
26 // exn = wasm.catch(WebAssembly::CPP_EXCEPTION);
27 // // Only add below in case it's not a single catch (...)
28 // wasm.landingpad.index(index);
29 // __wasm_lpad_context.lpad_index = index;
30 // __wasm_lpad_context.lsda = wasm.lsda();
31 // _Unwind_CallPersonality(exn);
32 // selector = __wasm_lpad_context.selector;
33 // ...
34 //
35 //
36 // * Background: Direct personality function call
37 // In WebAssembly EH, the VM is responsible for unwinding the stack once an
38 // exception is thrown. After the stack is unwound, the control flow is
39 // transfered to WebAssembly 'catch' instruction.
40 //
41 // Unwinding the stack is not done by libunwind but the VM, so the personality
42 // function in libcxxabi cannot be called from libunwind during the unwinding
43 // process. So after a catch instruction, we insert a call to a wrapper function
44 // in libunwind that in turn calls the real personality function.
45 //
46 // In Itanium EH, if the personality function decides there is no matching catch
47 // clause in a call frame and no cleanup action to perform, the unwinder doesn't
48 // stop there and continues unwinding. But in Wasm EH, the unwinder stops at
49 // every call frame with a catch intruction, after which the personality
50 // function is called from the compiler-generated user code here.
51 //
52 // In libunwind, we have this struct that serves as a communincation channel
53 // between the compiler-generated user code and the personality function in
54 // libcxxabi.
55 //
56 // struct _Unwind_LandingPadContext {
57 // uintptr_t lpad_index;
58 // uintptr_t lsda;
59 // uintptr_t selector;
60 // };
61 // struct _Unwind_LandingPadContext __wasm_lpad_context = ...;
62 //
63 // And this wrapper in libunwind calls the personality function.
64 //
65 // _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
66 // struct _Unwind_Exception *exception_obj =
67 // (struct _Unwind_Exception *)exception_ptr;
68 // _Unwind_Reason_Code ret = __gxx_personality_v0(
69 // 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj,
70 // (struct _Unwind_Context *)__wasm_lpad_context);
71 // return ret;
72 // }
73 //
74 // We pass a landing pad index, and the address of LSDA for the current function
75 // to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve
76 // the selector after it returns.
77 //
78 //===----------------------------------------------------------------------===//
79
80 #include "llvm/CodeGen/MachineBasicBlock.h"
81 #include "llvm/CodeGen/Passes.h"
82 #include "llvm/CodeGen/WasmEHFuncInfo.h"
83 #include "llvm/IR/IRBuilder.h"
84 #include "llvm/IR/IntrinsicsWebAssembly.h"
85 #include "llvm/InitializePasses.h"
86 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
87
88 using namespace llvm;
89
90 #define DEBUG_TYPE "wasmehprepare"
91
92 namespace {
93 class WasmEHPrepare : public FunctionPass {
94 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
95 GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
96
97 // Field addresses of struct _Unwind_LandingPadContext
98 Value *LPadIndexField = nullptr; // lpad_index field
99 Value *LSDAField = nullptr; // lsda field
100 Value *SelectorField = nullptr; // selector
101
102 Function *ThrowF = nullptr; // wasm.throw() intrinsic
103 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
104 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
105 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
106 Function *CatchF = nullptr; // wasm.catch() intrinsic
107 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
108 FunctionCallee CallPersonalityF =
109 nullptr; // _Unwind_CallPersonality() wrapper
110
111 bool prepareThrows(Function &F);
112 bool prepareEHPads(Function &F);
113 void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
114
115 public:
116 static char ID; // Pass identification, replacement for typeid
117
WasmEHPrepare()118 WasmEHPrepare() : FunctionPass(ID) {}
119 bool doInitialization(Module &M) override;
120 bool runOnFunction(Function &F) override;
121
getPassName() const122 StringRef getPassName() const override {
123 return "WebAssembly Exception handling preparation";
124 }
125 };
126 } // end anonymous namespace
127
128 char WasmEHPrepare::ID = 0;
129 INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE,
130 "Prepare WebAssembly exceptions", false, false)
131 INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
132 false, false)
133
createWasmEHPass()134 FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
135
doInitialization(Module & M)136 bool WasmEHPrepare::doInitialization(Module &M) {
137 IRBuilder<> IRB(M.getContext());
138 LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
139 IRB.getInt8PtrTy(), // lsda
140 IRB.getInt32Ty() // selector
141 );
142 return false;
143 }
144
145 // Erase the specified BBs if the BB does not have any remaining predecessors,
146 // and also all its dead children.
147 template <typename Container>
eraseDeadBBsAndChildren(const Container & BBs)148 static void eraseDeadBBsAndChildren(const Container &BBs) {
149 SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end());
150 while (!WL.empty()) {
151 auto *BB = WL.pop_back_val();
152 if (!pred_empty(BB))
153 continue;
154 WL.append(succ_begin(BB), succ_end(BB));
155 DeleteDeadBlock(BB);
156 }
157 }
158
runOnFunction(Function & F)159 bool WasmEHPrepare::runOnFunction(Function &F) {
160 bool Changed = false;
161 Changed |= prepareThrows(F);
162 Changed |= prepareEHPads(F);
163 return Changed;
164 }
165
prepareThrows(Function & F)166 bool WasmEHPrepare::prepareThrows(Function &F) {
167 Module &M = *F.getParent();
168 IRBuilder<> IRB(F.getContext());
169 bool Changed = false;
170
171 // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction.
172 ThrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_throw);
173 // Insert an unreachable instruction after a call to @llvm.wasm.throw and
174 // delete all following instructions within the BB, and delete all the dead
175 // children of the BB as well.
176 for (User *U : ThrowF->users()) {
177 // A call to @llvm.wasm.throw() is only generated from __cxa_throw()
178 // builtin call within libcxxabi, and cannot be an InvokeInst.
179 auto *ThrowI = cast<CallInst>(U);
180 if (ThrowI->getFunction() != &F)
181 continue;
182 Changed = true;
183 auto *BB = ThrowI->getParent();
184 SmallVector<BasicBlock *, 4> Succs(successors(BB));
185 auto &InstList = BB->getInstList();
186 InstList.erase(std::next(BasicBlock::iterator(ThrowI)), InstList.end());
187 IRB.SetInsertPoint(BB);
188 IRB.CreateUnreachable();
189 eraseDeadBBsAndChildren(Succs);
190 }
191
192 return Changed;
193 }
194
prepareEHPads(Function & F)195 bool WasmEHPrepare::prepareEHPads(Function &F) {
196 Module &M = *F.getParent();
197 IRBuilder<> IRB(F.getContext());
198
199 SmallVector<BasicBlock *, 16> CatchPads;
200 SmallVector<BasicBlock *, 16> CleanupPads;
201 for (BasicBlock &BB : F) {
202 if (!BB.isEHPad())
203 continue;
204 auto *Pad = BB.getFirstNonPHI();
205 if (isa<CatchPadInst>(Pad))
206 CatchPads.push_back(&BB);
207 else if (isa<CleanupPadInst>(Pad))
208 CleanupPads.push_back(&BB);
209 }
210 if (CatchPads.empty() && CleanupPads.empty())
211 return false;
212
213 assert(F.hasPersonalityFn() && "Personality function not found");
214
215 // __wasm_lpad_context global variable.
216 // This variable should be thread local. If the target does not support TLS,
217 // we depend on CoalesceFeaturesAndStripAtomics to downgrade it to
218 // non-thread-local ones, in which case we don't allow this object to be
219 // linked with other objects using shared memory.
220 LPadContextGV = cast<GlobalVariable>(
221 M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
222 LPadContextGV->setThreadLocalMode(GlobalValue::GeneralDynamicTLSModel);
223
224 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
225 "lpad_index_gep");
226 LSDAField =
227 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep");
228 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
229 "selector_gep");
230
231 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
232 LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index);
233 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
234 // function.
235 LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda);
236 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
237 // are generated in clang.
238 GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception);
239 GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector);
240
241 // wasm.catch() will be lowered down to wasm 'catch' instruction in
242 // instruction selection.
243 CatchF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_catch);
244
245 // _Unwind_CallPersonality() wrapper function, which calls the personality
246 CallPersonalityF = M.getOrInsertFunction(
247 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
248 if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
249 F->setDoesNotThrow();
250
251 unsigned Index = 0;
252 for (auto *BB : CatchPads) {
253 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
254 // In case of a single catch (...), we don't need to emit a personalify
255 // function call
256 if (CPI->getNumArgOperands() == 1 &&
257 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
258 prepareEHPad(BB, false);
259 else
260 prepareEHPad(BB, true, Index++);
261 }
262
263 // Cleanup pads don't need a personality function call.
264 for (auto *BB : CleanupPads)
265 prepareEHPad(BB, false);
266
267 return true;
268 }
269
270 // Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
271 // ignored.
prepareEHPad(BasicBlock * BB,bool NeedPersonality,unsigned Index)272 void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
273 unsigned Index) {
274 assert(BB->isEHPad() && "BB is not an EHPad!");
275 IRBuilder<> IRB(BB->getContext());
276 IRB.SetInsertPoint(&*BB->getFirstInsertionPt());
277
278 auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI());
279 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
280 for (auto &U : FPI->uses()) {
281 if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
282 if (CI->getCalledOperand() == GetExnF)
283 GetExnCI = CI;
284 if (CI->getCalledOperand() == GetSelectorF)
285 GetSelectorCI = CI;
286 }
287 }
288
289 // Cleanup pads do not have any of wasm.get.exception() or
290 // wasm.get.ehselector() calls. We need to do nothing.
291 if (!GetExnCI) {
292 assert(!GetSelectorCI &&
293 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
294 return;
295 }
296
297 // Replace wasm.get.exception intrinsic with wasm.catch intrinsic, which will
298 // be lowered to wasm 'catch' instruction. We do this mainly because
299 // instruction selection cannot handle wasm.get.exception intrinsic's token
300 // argument.
301 Instruction *CatchCI =
302 IRB.CreateCall(CatchF, {IRB.getInt32(WebAssembly::CPP_EXCEPTION)}, "exn");
303 GetExnCI->replaceAllUsesWith(CatchCI);
304 GetExnCI->eraseFromParent();
305
306 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
307 // need to call personality function because we don't need a selector.
308 if (!NeedPersonality) {
309 if (GetSelectorCI) {
310 assert(GetSelectorCI->use_empty() &&
311 "wasm.get.ehselector() still has uses!");
312 GetSelectorCI->eraseFromParent();
313 }
314 return;
315 }
316 IRB.SetInsertPoint(CatchCI->getNextNode());
317
318 // This is to create a map of <landingpad EH label, landingpad index> in
319 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
320 // Pseudocode: wasm.landingpad.index(Index);
321 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
322
323 // Pseudocode: __wasm_lpad_context.lpad_index = index;
324 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
325
326 auto *CPI = cast<CatchPadInst>(FPI);
327 // TODO Sometimes storing the LSDA address every time is not necessary, in
328 // case it is already set in a dominating EH pad and there is no function call
329 // between from that EH pad to here. Consider optimizing those cases.
330 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
331 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
332
333 // Pseudocode: _Unwind_CallPersonality(exn);
334 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
335 OperandBundleDef("funclet", CPI));
336 PersCI->setDoesNotThrow();
337
338 // Pseudocode: int selector = __wasm_lpad_context.selector;
339 Instruction *Selector =
340 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector");
341
342 // Replace the return value from wasm.get.ehselector() with the selector value
343 // loaded from __wasm_lpad_context.selector.
344 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
345 GetSelectorCI->replaceAllUsesWith(Selector);
346 GetSelectorCI->eraseFromParent();
347 }
348
calculateWasmEHInfo(const Function * F,WasmEHFuncInfo & EHInfo)349 void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) {
350 // If an exception is not caught by a catchpad (i.e., it is a foreign
351 // exception), it will unwind to its parent catchswitch's unwind destination.
352 // We don't record an unwind destination for cleanuppads because every
353 // exception should be caught by it.
354 for (const auto &BB : *F) {
355 if (!BB.isEHPad())
356 continue;
357 const Instruction *Pad = BB.getFirstNonPHI();
358
359 if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
360 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
361 if (!UnwindBB)
362 continue;
363 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
364 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
365 // Currently there should be only one handler per a catchswitch.
366 EHInfo.setUnwindDest(&BB, *CatchSwitch->handlers().begin());
367 else // cleanuppad
368 EHInfo.setUnwindDest(&BB, UnwindBB);
369 }
370 }
371 }
372