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// personality_fn(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// transferred to WebAssembly 'catch' instruction.
40//
41// Unwinding the stack is not done by libunwind but the VM, so the personality
42// function (e.g. in libcxxabi) cannot be called from libunwind during the
43// unwinding process. So after a catch instruction, we insert a direct call to
44// the personality instead.
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 instruction, 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 communication 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// We pass a landing pad index, and the address of LSDA for the current function
64// to the personality function, and we retrieve the selector after it returns.
65//
66//===----------------------------------------------------------------------===//
67
68#include "llvm/CodeGen/WasmEHPrepare.h"
69#include "llvm/CodeGen/MachineBasicBlock.h"
70#include "llvm/CodeGen/Passes.h"
71#include "llvm/CodeGen/WasmEHInfo.h"
72#include "llvm/IR/EHPersonalities.h"
73#include "llvm/IR/IRBuilder.h"
74#include "llvm/IR/IntrinsicsWebAssembly.h"
75#include "llvm/IR/Module.h"
76#include "llvm/IR/RuntimeLibcalls.h"
77#include "llvm/InitializePasses.h"
78#include "llvm/Transforms/Utils/BasicBlockUtils.h"
79
80using namespace llvm;
81
82#define DEBUG_TYPE "wasm-eh-prepare"
83
84namespace {
85class WasmEHPrepareImpl {
86 friend class WasmEHPrepare;
87
88 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
89
90 Function *ThrowF = nullptr; // wasm.throw() intrinsic
91 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
92 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
93 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
94 Function *CatchF = nullptr; // wasm.catch() intrinsic
95 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
96 FunctionCallee PersonalityF = nullptr;
97 FunctionCallee GetWasmLPadContextF =
98 nullptr; // _Unwind_GetWasmLPadContext() wrapper
99
100 bool prepareThrows(Function &F);
101 bool prepareEHPads(Function &F);
102 void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
103
104public:
105 WasmEHPrepareImpl() = default;
106 WasmEHPrepareImpl(Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
107 bool runOnFunction(Function &F);
108};
109
110class WasmEHPrepare : public FunctionPass {
111 WasmEHPrepareImpl P;
112
113public:
114 static char ID; // Pass identification, replacement for typeid
115
116 WasmEHPrepare() : FunctionPass(ID) {}
117 bool doInitialization(Module &M) override;
118 bool runOnFunction(Function &F) override { return P.runOnFunction(F); }
119
120 StringRef getPassName() const override {
121 return "WebAssembly Exception handling preparation";
122 }
123};
124
125} // end anonymous namespace
126
127PreservedAnalyses WasmEHPreparePass::run(Function &F,
128 FunctionAnalysisManager &) {
129 auto &Context = F.getContext();
130 auto *I32Ty = Type::getInt32Ty(C&: Context);
131 auto *PtrTy = PointerType::get(C&: Context, AddressSpace: 0);
132 auto *LPadContextTy =
133 StructType::get(elt1: I32Ty /*lpad_index*/, elts: PtrTy /*lsda*/, elts: I32Ty /*selector*/);
134 WasmEHPrepareImpl P(LPadContextTy);
135 bool Changed = P.runOnFunction(F);
136 return Changed ? PreservedAnalyses::none() : PreservedAnalyses ::all();
137}
138
139char WasmEHPrepare::ID = 0;
140INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE,
141 "Prepare WebAssembly exceptions", false, false)
142INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
143 false, false)
144
145FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
146
147bool WasmEHPrepare::doInitialization(Module &M) {
148 IRBuilder<> IRB(M);
149 P.LPadContextTy = StructType::get(elt1: IRB.getInt32Ty(), // lpad_index
150 elts: IRB.getPtrTy(), // lsda
151 elts: IRB.getInt32Ty() // selector
152 );
153 return false;
154}
155
156// Erase the specified BBs if the BB does not have any remaining predecessors,
157// and also all its dead children.
158template <typename Container>
159static void eraseDeadBBsAndChildren(const Container &BBs) {
160 SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end());
161 while (!WL.empty()) {
162 auto *BB = WL.pop_back_val();
163 if (!pred_empty(BB))
164 continue;
165 WL.append(in_start: succ_begin(BB), in_end: succ_end(BB));
166 DeleteDeadBlock(BB);
167 }
168}
169
170bool WasmEHPrepareImpl::runOnFunction(Function &F) {
171 if (F.getParent()->getExceptionModel() != ExceptionHandling::Wasm)
172 return false;
173
174 bool Changed = false;
175 Changed |= prepareThrows(F);
176 Changed |= prepareEHPads(F);
177 return Changed;
178}
179
180bool WasmEHPrepareImpl::prepareThrows(Function &F) {
181 Module &M = *F.getParent();
182 IRBuilder<> IRB(M);
183 bool Changed = false;
184
185 // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction.
186 ThrowF = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_throw);
187 // Insert an unreachable instruction after a call to @llvm.wasm.throw and
188 // delete all following instructions within the BB, and delete all the dead
189 // children of the BB as well.
190 for (User *U : ThrowF->users()) {
191 auto *ThrowI = dyn_cast<CallInst>(Val: U);
192 if (!ThrowI || ThrowI->getFunction() != &F)
193 continue;
194 Changed = true;
195 auto *BB = ThrowI->getParent();
196 SmallVector<BasicBlock *, 4> Succs(successors(BB));
197 BB->erase(FromIt: std::next(x: BasicBlock::iterator(ThrowI)), ToIt: BB->end());
198 IRB.SetInsertPoint(BB);
199 IRB.CreateUnreachable();
200 eraseDeadBBsAndChildren(BBs: Succs);
201 }
202
203 return Changed;
204}
205
206bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
207 Module &M = *F.getParent();
208 LLVMContext &Ctx = M.getContext();
209
210 SmallVector<BasicBlock *, 16> CatchPads;
211 SmallVector<BasicBlock *, 16> CleanupPads;
212 for (BasicBlock &BB : F) {
213 if (!BB.isEHPad())
214 continue;
215 BasicBlock::iterator Pad = BB.getFirstNonPHIIt();
216 if (isa<CatchPadInst>(Val: Pad))
217 CatchPads.push_back(Elt: &BB);
218 else if (isa<CleanupPadInst>(Val: Pad))
219 CleanupPads.push_back(Elt: &BB);
220 }
221 if (CatchPads.empty() && CleanupPads.empty())
222 return false;
223
224 if (!F.hasPersonalityFn())
225 return false;
226
227 auto Personality = classifyEHPersonality(Pers: F.getPersonalityFn());
228
229 if (!isScopedEHPersonality(Pers: Personality)) {
230 report_fatal_error(reason: "Function '" + F.getName() +
231 "' does not have a supported Wasm personality function");
232 }
233 assert(F.hasPersonalityFn() && "Personality function not found");
234
235 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
236 LPadIndexF =
237 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_landingpad_index);
238 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
239 // function.
240 LSDAF = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_lsda);
241 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
242 // are generated in clang.
243 GetExnF =
244 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_get_exception);
245 GetSelectorF =
246 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_get_ehselector);
247
248 // wasm.catch() will be lowered down to wasm 'catch' instruction in
249 // instruction selection.
250 CatchF = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_catch);
251
252 auto *PersPrototype = FunctionType::get(Result: Type::getInt32Ty(C&: Ctx),
253 Params: {PointerType::getUnqual(C&: Ctx)}, isVarArg: false);
254 PersonalityF =
255 M.getOrInsertFunction(Name: getEHPersonalityName(Pers: Personality), T: PersPrototype);
256
257 if (Function *F = dyn_cast<Function>(Val: PersonalityF.getCallee()))
258 F->setDoesNotThrow();
259
260 StringRef UnwindGetWasmLPadContextName =
261 RTLIB::RuntimeLibcallsInfo::getLibcallImplName(
262 CallImpl: RTLIB::impl__Unwind_GetWasmLPadContext);
263
264 // _Unwind_GetWasmLPadContext() wrapper function
265 //
266 // We use this function to get the address of `libunwind`'s thread-local
267 // `__wasm_lpad_context` variable for the current thread. Note that we
268 // cannot, in general, access the `__wasm_lpad_context` directly here because,
269 // when the cooperative multithreading feature is enabled, direct,
270 // cross-library access to thread local variables is not supported.
271 auto *UnwindGetWasmLPadContextType =
272 FunctionType::get(Result: PointerType::getUnqual(C&: Ctx), Params: {}, isVarArg: false);
273 GetWasmLPadContextF = M.getOrInsertFunction(Name: UnwindGetWasmLPadContextName,
274 T: UnwindGetWasmLPadContextType);
275 if (Function *F = dyn_cast<Function>(Val: GetWasmLPadContextF.getCallee()))
276 F->setDoesNotThrow();
277
278 unsigned Index = 0;
279 for (auto *BB : CatchPads) {
280 auto *CPI = cast<CatchPadInst>(Val: BB->getFirstNonPHIIt());
281 // In case of a single catch (...), we don't need to emit a personalify
282 // function call
283 if (CPI->arg_size() == 1 &&
284 cast<Constant>(Val: CPI->getArgOperand(i: 0))->isNullValue())
285 prepareEHPad(BB, NeedPersonality: false);
286 else
287 prepareEHPad(BB, NeedPersonality: true, Index: Index++);
288 }
289
290 // Cleanup pads don't need a personality function call.
291 for (auto *BB : CleanupPads)
292 prepareEHPad(BB, NeedPersonality: false);
293
294 return true;
295}
296
297// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
298// ignored.
299void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
300 unsigned Index) {
301 assert(BB->isEHPad() && "BB is not an EHPad!");
302 IRBuilder<> IRB(BB->getFirstInsertionPt());
303
304 auto *FPI = cast<FuncletPadInst>(Val: BB->getFirstNonPHIIt());
305 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
306 for (auto &U : FPI->uses()) {
307 if (auto *CI = dyn_cast<CallInst>(Val: U.getUser())) {
308 if (CI->getCalledOperand() == GetExnF)
309 GetExnCI = CI;
310 if (CI->getCalledOperand() == GetSelectorF)
311 GetSelectorCI = CI;
312 }
313 }
314
315 // Cleanup pads do not have any of wasm.get.exception() or
316 // wasm.get.ehselector() calls. We need to do nothing.
317 if (!GetExnCI) {
318 assert(!GetSelectorCI &&
319 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
320 return;
321 }
322
323 // Replace wasm.get.exception intrinsic with wasm.catch intrinsic, which will
324 // be lowered to wasm 'catch' instruction. We do this mainly because
325 // instruction selection cannot handle wasm.get.exception intrinsic's token
326 // argument.
327 Instruction *CatchCI =
328 IRB.CreateCall(Callee: CatchF, Args: {IRB.getInt32(C: WebAssembly::CPP_EXCEPTION)}, Name: "exn");
329 GetExnCI->replaceAllUsesWith(V: CatchCI);
330 GetExnCI->eraseFromParent();
331
332 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
333 // need to call personality function because we don't need a selector.
334 if (!NeedPersonality) {
335 if (GetSelectorCI) {
336 assert(GetSelectorCI->use_empty() &&
337 "wasm.get.ehselector() still has uses!");
338 GetSelectorCI->eraseFromParent();
339 }
340 return;
341 }
342 IRB.SetInsertPoint(CatchCI->getNextNode());
343
344 Instruction *LPadContext =
345 IRB.CreateCall(Callee: GetWasmLPadContextF, Args: {}, OpBundles: OperandBundleDef("funclet", FPI));
346
347 Value *LPadIndexField = LPadContext;
348 Value *LSDAField = IRB.CreateConstInBoundsGEP2_32(Ty: LPadContextTy, Ptr: LPadContext,
349 Idx0: 0, Idx1: 1, Name: "lsda_gep");
350 Value *SelectorField = IRB.CreateConstInBoundsGEP2_32(
351 Ty: LPadContextTy, Ptr: LPadContext, Idx0: 0, Idx1: 2, Name: "selector_gep");
352
353 // This is to create a map of <landingpad EH label, landingpad index> in
354 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
355 // Pseudocode: wasm.landingpad.index(Index);
356 IRB.CreateCall(Callee: LPadIndexF, Args: {FPI, IRB.getInt32(C: Index)});
357
358 // Pseudocode: __wasm_lpad_context.lpad_index = index;
359 IRB.CreateStore(Val: IRB.getInt32(C: Index), Ptr: LPadIndexField);
360
361 // TODO Sometimes storing the LSDA address every time is not necessary, in
362 // case it is already set in a dominating EH pad and there is no function call
363 // between from that EH pad to here. Consider optimizing those cases.
364 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
365 IRB.CreateStore(Val: IRB.CreateCall(Callee: LSDAF), Ptr: LSDAField);
366
367 // Pseudocode: personality_fn(exn);
368 CallInst *PersCI =
369 IRB.CreateCall(Callee: PersonalityF, Args: CatchCI, OpBundles: OperandBundleDef("funclet", FPI));
370 PersCI->setDoesNotThrow();
371
372 // Pseudocode: int selector = __wasm_lpad_context.selector;
373 Instruction *Selector =
374 IRB.CreateLoad(Ty: IRB.getInt32Ty(), Ptr: SelectorField, Name: "selector");
375
376 // Replace the return value from wasm.get.ehselector() with the selector value
377 // loaded from __wasm_lpad_context.selector.
378 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
379 GetSelectorCI->replaceAllUsesWith(V: Selector);
380 GetSelectorCI->eraseFromParent();
381}
382