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