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