|
| 1 | +// WebAssemblyHandleEHTerminatePads.cpp - WebAssembly Handle EH TerminatePads // |
| 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 | +/// \file |
| 10 | +/// \brief Add catch_all blocks to terminate pads. |
| 11 | +/// |
| 12 | +/// Terminate pads are cleanup pads with a __clang_call_terminate call. These |
| 13 | +/// are reached when an exception is thrown again in the middle of processing a |
| 14 | +/// thrown exception, to terminate the program. These are cleanup pads that |
| 15 | +/// should run regardless whether the thrown exception is a C++ exception or |
| 16 | +/// not. |
| 17 | +/// |
| 18 | +/// Because __clang_call_terminate takes an exception pointer, and |
| 19 | +/// llvm.get.exception intrinsic is selected to 'catch' instruction in |
| 20 | +/// instruction selection, terminate pads have a catch instruction and are in |
| 21 | +/// this form after LateEHPrepare, even though they are cleanup pads: |
| 22 | +/// termpad: |
| 23 | +/// %exn = catch $__cpp_exception |
| 24 | +/// call @__clang_call_terminate(%exn) |
| 25 | +/// unreachable |
| 26 | +/// |
| 27 | +/// This pass assumes LateEHPrepare ensured every terminate pad is a single |
| 28 | +/// BB. |
| 29 | +/// |
| 30 | +/// __clang_call_terminate is a function generated by clang, in the form of |
| 31 | +/// void __clang_call_terminate(i8* %arg) { |
| 32 | +/// call @__cxa_begin_catch(%arg) |
| 33 | +/// call void @std::terminate() |
| 34 | +/// unreachable |
| 35 | +/// } |
| 36 | +/// |
| 37 | +/// To make the terminate pads reachable when a foreign exception is thrown, |
| 38 | +/// this pass attaches an additional catch_all BB after this catch terminate pad |
| 39 | +/// BB, with a call to std::terminate, because foreign exceptions don't have a |
| 40 | +/// valid exception pointer to call __cxa_begin_catch with. So the code example |
| 41 | +/// becomes: |
| 42 | +/// termpad: |
| 43 | +/// %exn = catch $__cpp_exception |
| 44 | +/// call @__clang_call_terminate(%exn) |
| 45 | +/// unreachable |
| 46 | +/// termpad-catchall: |
| 47 | +/// catch_all |
| 48 | +/// call @std::terminate() |
| 49 | +/// unreachable |
| 50 | +/// |
| 51 | +/// We do this at the very end of compilation pipeline, even after CFGStackify, |
| 52 | +/// because even though wasm spec allows multiple catch/catch_all blocks per a |
| 53 | +/// try instruction, it has been convenient to maintain the invariant so far |
| 54 | +/// that there has been only a single catch or catch_all attached to a try. This |
| 55 | +/// assumption makes ExceptionInfo generation and CFGStackify simpler, because |
| 56 | +/// we have been always able to assume an EH pad is an end of try block and a |
| 57 | +/// start of catch/catch_all block. |
| 58 | +//===----------------------------------------------------------------------===// |
| 59 | + |
| 60 | +#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 61 | +#include "WebAssembly.h" |
| 62 | +#include "WebAssemblySubtarget.h" |
| 63 | +#include "WebAssemblyUtilities.h" |
| 64 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 65 | +#include "llvm/MC/MCAsmInfo.h" |
| 66 | +#include "llvm/Target/TargetMachine.h" |
| 67 | +using namespace llvm; |
| 68 | + |
| 69 | +#define DEBUG_TYPE "wasm-handle-termpads" |
| 70 | + |
| 71 | +namespace { |
| 72 | +class WebAssemblyHandleEHTerminatePads final : public MachineFunctionPass { |
| 73 | + StringRef getPassName() const override { |
| 74 | + return "WebAssembly Handle EH Terminate Pads"; |
| 75 | + } |
| 76 | + |
| 77 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 78 | + |
| 79 | +public: |
| 80 | + static char ID; // Pass identification, replacement for typeid |
| 81 | + WebAssemblyHandleEHTerminatePads() : MachineFunctionPass(ID) {} |
| 82 | +}; |
| 83 | +} // end anonymous namespace |
| 84 | + |
| 85 | +char WebAssemblyHandleEHTerminatePads::ID = 0; |
| 86 | +INITIALIZE_PASS(WebAssemblyHandleEHTerminatePads, DEBUG_TYPE, |
| 87 | + "WebAssembly Handle EH Terminate Pads", false, false) |
| 88 | + |
| 89 | +FunctionPass *llvm::createWebAssemblyHandleEHTerminatePads() { |
| 90 | + return new WebAssemblyHandleEHTerminatePads(); |
| 91 | +} |
| 92 | + |
| 93 | +bool WebAssemblyHandleEHTerminatePads::runOnMachineFunction( |
| 94 | + MachineFunction &MF) { |
| 95 | + LLVM_DEBUG(dbgs() << "********** Handle EH Terminate Pads **********\n" |
| 96 | + "********** Function: " |
| 97 | + << MF.getName() << '\n'); |
| 98 | + |
| 99 | + if (MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() != |
| 100 | + ExceptionHandling::Wasm || |
| 101 | + !MF.getFunction().hasPersonalityFn()) |
| 102 | + return false; |
| 103 | + |
| 104 | + const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 105 | + |
| 106 | + // Find calls to __clang_call_terminate() |
| 107 | + SmallVector<MachineInstr *, 8> ClangCallTerminateCalls; |
| 108 | + for (auto &MBB : MF) { |
| 109 | + for (auto &MI : MBB) { |
| 110 | + if (MI.isCall()) { |
| 111 | + const MachineOperand &CalleeOp = MI.getOperand(0); |
| 112 | + if (CalleeOp.isGlobal() && CalleeOp.getGlobal()->getName() == |
| 113 | + WebAssembly::ClangCallTerminateFn) |
| 114 | + ClangCallTerminateCalls.push_back(&MI); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (ClangCallTerminateCalls.empty()) |
| 120 | + return false; |
| 121 | + |
| 122 | + for (auto *Call : ClangCallTerminateCalls) { |
| 123 | + // This should be an EH pad because LateEHPrepare ensures terminate pads are |
| 124 | + // a single BB. |
| 125 | + MachineBasicBlock *CatchBB = Call->getParent(); |
| 126 | + assert(CatchBB->isEHPad()); |
| 127 | + |
| 128 | + auto *CatchAllBB = MF.CreateMachineBasicBlock(); |
| 129 | + MF.insert(std::next(CatchBB->getIterator()), CatchAllBB); |
| 130 | + CatchAllBB->setIsEHPad(true); |
| 131 | + for (auto *Pred : CatchBB->predecessors()) |
| 132 | + Pred->addSuccessor(CatchAllBB); |
| 133 | + |
| 134 | + // If the definition of __clang_call_terminate exists in the module, there |
| 135 | + // should be a declaration of std::terminate within the same module, because |
| 136 | + // __clang_call_terminate calls it. |
| 137 | + const auto *StdTerminateFn = |
| 138 | + MF.getMMI().getModule()->getNamedValue(WebAssembly::StdTerminateFn); |
| 139 | + assert(StdTerminateFn && "std::terminate() does not exist in the module"); |
| 140 | + |
| 141 | + // Generate a BB in the form of: |
| 142 | + // catch_all |
| 143 | + // call @std::terminate |
| 144 | + // unreachable |
| 145 | + BuildMI(CatchAllBB, Call->getDebugLoc(), TII.get(WebAssembly::CATCH_ALL)); |
| 146 | + BuildMI(CatchAllBB, Call->getDebugLoc(), TII.get(WebAssembly::CALL)) |
| 147 | + .addGlobalAddress(StdTerminateFn); |
| 148 | + BuildMI(CatchAllBB, Call->getDebugLoc(), TII.get(WebAssembly::UNREACHABLE)); |
| 149 | + } |
| 150 | + |
| 151 | + return true; |
| 152 | +} |
0 commit comments