1//=== WebAssemblyLowerEmscriptenEHSjLj.cpp - Lower exceptions for Emscripten =//
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/// This file lowers exception-related instructions and setjmp/longjmp function
11/// calls to use Emscripten's library functions. The pass uses JavaScript's try
12/// and catch mechanism in case of Emscripten EH/SjLj and Wasm EH intrinsics in
13/// case of Emscripten SjLJ.
14///
15/// * Emscripten exception handling
16/// This pass lowers invokes and landingpads into library functions in JS glue
17/// code. Invokes are lowered into function wrappers called invoke wrappers that
18/// exist in JS side, which wraps the original function call with JS try-catch.
19/// If an exception occurred, cxa_throw() function in JS side sets some
20/// variables (see below) so we can check whether an exception occurred from
21/// wasm code and handle it appropriately.
22///
23/// * Emscripten setjmp-longjmp handling
24/// This pass lowers setjmp to a reasonably-performant approach for emscripten.
25/// The idea is that each block with a setjmp is broken up into two parts: the
26/// part containing setjmp and the part right after the setjmp. The latter part
27/// is either reached from the setjmp, or later from a longjmp. To handle the
28/// longjmp, all calls that might longjmp are also called using invoke wrappers
29/// and thus JS / try-catch. JS longjmp() function also sets some variables so
30/// we can check / whether a longjmp occurred from wasm code. Each block with a
31/// function call that might longjmp is also split up after the longjmp call.
32/// After the longjmp call, we check whether a longjmp occurred, and if it did,
33/// which setjmp it corresponds to, and jump to the right post-setjmp block.
34/// We assume setjmp-longjmp handling always run after EH handling, which means
35/// we don't expect any exception-related instructions when SjLj runs.
36/// FIXME Currently this scheme does not support indirect call of setjmp,
37/// because of the limitation of the scheme itself. fastcomp does not support it
38/// either.
39///
40/// In detail, this pass does following things:
41///
42/// 1) Assumes the existence of global variables: __THREW__, __threwValue
43/// __THREW__ and __threwValue are defined in compiler-rt in Emscripten.
44/// These variables are used for both exceptions and setjmp/longjmps.
45/// __THREW__ indicates whether an exception or a longjmp occurred or not. 0
46/// means nothing occurred, 1 means an exception occurred, and other numbers
47/// mean a longjmp occurred. In the case of longjmp, __THREW__ variable
48/// indicates the corresponding setjmp buffer the longjmp corresponds to.
49/// __threwValue is 0 for exceptions, and the argument to longjmp in case of
50/// longjmp.
51///
52/// * Emscripten exception handling
53///
54/// 2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions
55/// at link time. setThrew exists in Emscripten's compiler-rt:
56///
57/// void setThrew(uintptr_t threw, int value) {
58/// if (__THREW__ == 0) {
59/// __THREW__ = threw;
60/// __threwValue = value;
61/// }
62/// }
63//
64/// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
65/// In exception handling, getTempRet0 indicates the type of an exception
66/// caught, and in setjmp/longjmp, it means the second argument to longjmp
67/// function.
68///
69/// 3) Lower
70/// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
71/// into
72/// __THREW__ = 0;
73/// call @__invoke_SIG(func, arg1, arg2)
74/// %__THREW__.val = __THREW__;
75/// __THREW__ = 0;
76/// if (%__THREW__.val == 1)
77/// goto %lpad
78/// else
79/// goto %invoke.cont
80/// SIG is a mangled string generated based on the LLVM IR-level function
81/// signature. After LLVM IR types are lowered to the target wasm types,
82/// the names for these wrappers will change based on wasm types as well,
83/// as in invoke_vi (function takes an int and returns void). The bodies of
84/// these wrappers will be generated in JS glue code, and inside those
85/// wrappers we use JS try-catch to generate actual exception effects. It
86/// also calls the original callee function. An example wrapper in JS code
87/// would look like this:
88/// function invoke_vi(index,a1) {
89/// try {
90/// Module["dynCall_vi"](index,a1); // This calls original callee
91/// } catch(e) {
92/// if (typeof e !== 'number' && e !== 'longjmp') throw e;
93/// _setThrew(1, 0); // setThrew is called here
94/// }
95/// }
96/// If an exception is thrown, __THREW__ will be set to true in a wrapper,
97/// so we can jump to the right BB based on this value.
98///
99/// 4) Lower
100/// %val = landingpad catch c1 catch c2 catch c3 ...
101/// ... use %val ...
102/// into
103/// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...)
104/// %val = {%fmc, getTempRet0()}
105/// ... use %val ...
106/// Here N is a number calculated based on the number of clauses.
107/// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
108///
109/// 5) Lower
110/// resume {%a, %b}
111/// into
112/// call @__resumeException(%a)
113/// where __resumeException() is a function in JS glue code.
114///
115/// 6) Lower
116/// call @llvm.eh.typeid.for(type) (intrinsic)
117/// into
118/// call @llvm_eh_typeid_for(type)
119/// llvm_eh_typeid_for function will be generated in JS glue code.
120///
121/// * Emscripten setjmp / longjmp handling
122///
123/// If there are calls to longjmp()
124///
125/// 1) Lower
126/// longjmp(env, val)
127/// into
128/// emscripten_longjmp(env, val)
129///
130/// If there are calls to setjmp()
131///
132/// 2) In the function entry that calls setjmp, initialize
133/// functionInvocationId as follows:
134///
135/// functionInvocationId = alloca(4)
136///
137/// Note: the alloca size is not important as this pointer is
138/// merely used for pointer comparisons.
139///
140/// 3) Lower
141/// setjmp(env)
142/// into
143/// __wasm_setjmp(env, label, functionInvocationId)
144///
145/// __wasm_setjmp records the necessary info (the label and
146/// functionInvocationId) to the "env".
147/// A BB with setjmp is split into two after setjmp call in order to
148/// make the post-setjmp BB the possible destination of longjmp BB.
149///
150/// 4) Lower every call that might longjmp into
151/// __THREW__ = 0;
152/// call @__invoke_SIG(func, arg1, arg2)
153/// %__THREW__.val = __THREW__;
154/// __THREW__ = 0;
155/// %__threwValue.val = __threwValue;
156/// if (%__THREW__.val != 0 & %__threwValue.val != 0) {
157/// %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
158/// if (%label == 0)
159/// emscripten_longjmp(%__THREW__.val, %__threwValue.val);
160/// setTempRet0(%__threwValue.val);
161/// } else {
162/// %label = -1;
163/// }
164/// longjmp_result = getTempRet0();
165/// switch %label {
166/// label 1: goto post-setjmp BB 1
167/// label 2: goto post-setjmp BB 2
168/// ...
169/// default: goto split next BB
170/// }
171///
172/// __wasm_setjmp_test examines the jmp buf to see if it was for a matching
173/// setjmp call. After calling an invoke wrapper, if a longjmp occurred,
174/// __THREW__ will be the address of matching jmp_buf buffer and
175/// __threwValue be the second argument to longjmp.
176/// __wasm_setjmp_test returns a setjmp label, a unique ID to each setjmp
177/// callsite. Label 0 means this longjmp buffer does not correspond to one
178/// of the setjmp callsites in this function, so in this case we just chain
179/// the longjmp to the caller. Label -1 means no longjmp occurred.
180/// Otherwise we jump to the right post-setjmp BB based on the label.
181///
182/// * Wasm setjmp / longjmp handling
183/// This mode still uses some Emscripten library functions but not JavaScript's
184/// try-catch mechanism. It instead uses Wasm exception handling intrinsics,
185/// which will be lowered to exception handling instructions.
186///
187/// If there are calls to longjmp()
188///
189/// 1) Lower
190/// longjmp(env, val)
191/// into
192/// __wasm_longjmp(env, val)
193///
194/// If there are calls to setjmp()
195///
196/// 2) and 3): The same as 2) and 3) in Emscripten SjLj.
197/// (functionInvocationId initialization + setjmp callsite transformation)
198///
199/// 4) Create a catchpad with a wasm.catch() intrinsic, which returns the value
200/// thrown by __wasm_longjmp function. In the runtime library, we have an
201/// equivalent of the following struct:
202///
203/// struct __WasmLongjmpArgs {
204/// void *env;
205/// int val;
206/// };
207///
208/// The thrown value here is a pointer to the struct. We use this struct to
209/// transfer two values by throwing a single value. Wasm throw and catch
210/// instructions are capable of throwing and catching multiple values, but
211/// it also requires multivalue support that is currently not very reliable.
212/// TODO Switch to throwing and catching two values without using the struct
213///
214/// All longjmpable function calls will be converted to an invoke that will
215/// unwind to this catchpad in case a longjmp occurs. Within the catchpad, we
216/// test the thrown values using __wasm_setjmp_test function as we do for
217/// Emscripten SjLj. The main difference is, in Emscripten SjLj, we need to
218/// transform every longjmpable callsite into a sequence of code including
219/// __wasm_setjmp_test() call; in Wasm SjLj we do the testing in only one
220/// place, in this catchpad.
221///
222/// After testing calling __wasm_setjmp_test(), if the longjmp does not
223/// correspond to one of the setjmps within the current function, it rethrows
224/// the longjmp by calling __wasm_longjmp(). If it corresponds to one of
225/// setjmps in the function, we jump to the beginning of the function, which
226/// contains a switch to each post-setjmp BB. Again, in Emscripten SjLj, this
227/// switch is added for every longjmpable callsite; in Wasm SjLj we do this
228/// only once at the top of the function. (after functionInvocationId
229/// initialization)
230///
231/// The below is the pseudocode for what we have described
232///
233/// entry:
234/// Initialize functionInvocationId
235///
236/// setjmp.dispatch:
237/// switch %label {
238/// label 1: goto post-setjmp BB 1
239/// label 2: goto post-setjmp BB 2
240/// ...
241/// default: goto split next BB
242/// }
243/// ...
244///
245/// bb:
246/// invoke void @foo() ;; foo is a longjmpable function
247/// to label %next unwind label %catch.dispatch.longjmp
248/// ...
249///
250/// catch.dispatch.longjmp:
251/// %0 = catchswitch within none [label %catch.longjmp] unwind to caller
252///
253/// catch.longjmp:
254/// %longjmp.args = wasm.catch() ;; struct __WasmLongjmpArgs
255/// %env = load 'env' field from __WasmLongjmpArgs
256/// %val = load 'val' field from __WasmLongjmpArgs
257/// %label = __wasm_setjmp_test(%env, functionInvocationId);
258/// if (%label == 0)
259/// __wasm_longjmp(%env, %val)
260/// catchret to %setjmp.dispatch
261///
262///===----------------------------------------------------------------------===//
263
264#include "WebAssembly.h"
265#include "WebAssemblyTargetMachine.h"
266#include "llvm/ADT/MapVector.h"
267#include "llvm/ADT/StringExtras.h"
268#include "llvm/CodeGen/MachineModuleInfo.h"
269#include "llvm/CodeGen/WasmEHInfo.h"
270#include "llvm/IR/Analysis.h"
271#include "llvm/IR/DebugInfoMetadata.h"
272#include "llvm/IR/Dominators.h"
273#include "llvm/IR/IRBuilder.h"
274#include "llvm/IR/IntrinsicsWebAssembly.h"
275#include "llvm/IR/Module.h"
276#include "llvm/IR/PassManager.h"
277#include "llvm/Pass.h"
278#include "llvm/Support/CommandLine.h"
279#include "llvm/Transforms/Utils/BasicBlockUtils.h"
280#include "llvm/Transforms/Utils/Local.h"
281#include "llvm/Transforms/Utils/SSAUpdater.h"
282#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
283#include <set>
284
285using namespace llvm;
286
287#define DEBUG_TYPE "wasm-lower-em-ehsjlj"
288
289static cl::list<std::string>
290 EHAllowlist("emscripten-cxx-exceptions-allowed",
291 cl::desc("The list of function names in which Emscripten-style "
292 "exception handling is enabled (see emscripten "
293 "EMSCRIPTEN_CATCHING_ALLOWED options)"),
294 cl::CommaSeparated);
295
296namespace {
297class WebAssemblyLowerEmscriptenEHSjLjImpl {
298 bool EnableEmEH; // Enable Emscripten exception handling
299 bool EnableEmSjLj; // Enable Emscripten setjmp/longjmp handling
300 bool EnableWasmSjLj; // Enable Wasm setjmp/longjmp handling
301 bool DoSjLj; // Whether we actually perform setjmp/longjmp handling
302
303 GlobalVariable *ThrewGV = nullptr; // __THREW__ (Emscripten)
304 GlobalVariable *ThrewValueGV = nullptr; // __threwValue (Emscripten)
305 Function *GetTempRet0F = nullptr; // getTempRet0() (Emscripten)
306 Function *SetTempRet0F = nullptr; // setTempRet0() (Emscripten)
307 Function *ResumeF = nullptr; // __resumeException() (Emscripten)
308 Function *EHTypeIDF = nullptr; // llvm.eh.typeid.for() (intrinsic)
309 Function *EmLongjmpF = nullptr; // emscripten_longjmp() (Emscripten)
310 Function *WasmSetjmpF = nullptr; // __wasm_setjmp() (Emscripten)
311 Function *WasmSetjmpTestF = nullptr; // __wasm_setjmp_test() (Emscripten)
312 Function *WasmLongjmpF = nullptr; // __wasm_longjmp() (Emscripten)
313 Function *CatchF = nullptr; // wasm.catch() (intrinsic)
314
315 // type of 'struct __WasmLongjmpArgs' defined in emscripten
316 Type *LongjmpArgsTy = nullptr;
317
318 // __cxa_find_matching_catch_N functions.
319 // Indexed by the number of clauses in an original landingpad instruction.
320 DenseMap<int, Function *> FindMatchingCatches;
321 // Map of <function signature string, invoke_ wrappers>
322 StringMap<Function *> InvokeWrappers;
323 // Set of allowed function names for exception handling
324 std::set<std::string, std::less<>> EHAllowlistSet;
325 // Functions that contains calls to setjmp
326 SmallPtrSet<Function *, 8> SetjmpUsers;
327
328 std::function<DominatorTree &(Function &F)> GetDominatorTree;
329
330 using InstVector = SmallVectorImpl<Instruction *>;
331 bool runEHOnFunction(Function &F);
332 bool runSjLjOnFunction(Function &F);
333 void handleLongjmpableCallsForEmscriptenSjLj(
334 Function &F, Instruction *FunctionInvocationId,
335 SmallVectorImpl<PHINode *> &SetjmpRetPHIs);
336 void
337 handleLongjmpableCallsForWasmSjLj(Function &F,
338 Instruction *FunctionInvocationId,
339 SmallVectorImpl<PHINode *> &SetjmpRetPHIs);
340 Function *getFindMatchingCatch(Module &M, unsigned NumClauses);
341
342 Value *wrapInvoke(CallBase *CI);
343 void wrapTestSetjmp(BasicBlock *BB, DebugLoc DL, Value *Threw,
344 Value *FunctionInvocationId, Value *&Label,
345 Value *&LongjmpResult, BasicBlock *&CallEmLongjmpBB,
346 PHINode *&CallEmLongjmpBBThrewPHI,
347 PHINode *&CallEmLongjmpBBThrewValuePHI,
348 BasicBlock *&EndBB);
349 Function *getInvokeWrapper(CallBase *CI);
350
351 bool areAllExceptionsAllowed() const { return EHAllowlistSet.empty(); }
352 bool supportsException(const Function *F) const {
353 return EnableEmEH &&
354 (areAllExceptionsAllowed() || EHAllowlistSet.count(x: F->getName()));
355 }
356 void replaceLongjmpWith(Function *LongjmpF, Function *NewF);
357
358 void rebuildSSA(Function &F);
359
360public:
361 WebAssemblyLowerEmscriptenEHSjLjImpl(
362 bool EnableEmEH,
363 std::function<DominatorTree &(Function &F)> GetDominatorTree)
364 : EnableEmEH(EnableEmEH), EnableEmSjLj(WebAssembly::WasmEnableEmSjLj),
365 EnableWasmSjLj(WebAssembly::WasmEnableSjLj),
366 GetDominatorTree(GetDominatorTree) {
367 assert(!(EnableEmSjLj && EnableWasmSjLj) &&
368 "Two SjLj modes cannot be turned on at the same time");
369 assert(!(EnableEmEH && EnableWasmSjLj) &&
370 "Wasm SjLj should be only used with Wasm EH");
371 EHAllowlistSet.insert(first: EHAllowlist.begin(), last: EHAllowlist.end());
372 }
373
374 bool runOnModule(Module &M);
375};
376
377class WebAssemblyLowerEmscriptenEHSjLjLegacy final : public ModulePass {
378 bool EnableEmEH;
379
380 StringRef getPassName() const override {
381 return "WebAssembly Lower Emscripten Exceptions";
382 }
383
384public:
385 static char ID;
386
387 WebAssemblyLowerEmscriptenEHSjLjLegacy(bool EnableEmEH = false)
388 : ModulePass(ID), EnableEmEH(EnableEmEH) {}
389 bool runOnModule(Module &M) override;
390
391 void getAnalysisUsage(AnalysisUsage &AU) const override {
392 AU.addRequired<DominatorTreeWrapperPass>();
393 }
394};
395} // End anonymous namespace
396
397char WebAssemblyLowerEmscriptenEHSjLjLegacy::ID = 0;
398INITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLjLegacy, DEBUG_TYPE,
399 "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp",
400 false, false)
401
402ModulePass *
403llvm::createWebAssemblyLowerEmscriptenEHSjLjLegacyPass(bool EnableEmEH) {
404 return new WebAssemblyLowerEmscriptenEHSjLjLegacy(EnableEmEH);
405}
406
407static bool canThrow(const Value *V) {
408 if (const auto *F = dyn_cast<const Function>(Val: V)) {
409 StringRef Name = F->getName();
410 // leave setjmp and longjmp (mostly) alone, we process them properly later
411 if (Name == "setjmp" || Name == "longjmp" || Name == "emscripten_longjmp")
412 return false;
413 return !F->doesNotThrow();
414 }
415 // not a function, so an indirect call - can throw, we can't tell
416 return true;
417}
418
419// Get a thread-local global variable with the given name. If it doesn't exist
420// declare it, which will generate an import and assume that it will exist at
421// link time.
422static GlobalVariable *getGlobalVariable(Module &M, Type *Ty,
423 const char *Name) {
424 // Variables created by this function are thread local. If the target does not
425 // support TLS, we depend on CoalesceFeaturesAndStripAtomics to downgrade it
426 // to non-thread-local ones, in which case we don't allow this object to be
427 // linked with other objects using shared memory.
428 return M.getOrInsertGlobal(Name, Ty, CreateGlobalCallback: [&]() {
429 return new GlobalVariable(
430 M, Ty, /*isConstant=*/false, GlobalVariable::ExternalLinkage,
431 /*Initializer=*/nullptr, Name,
432 /*InsertBefore=*/nullptr, GlobalValue::GeneralDynamicTLSModel);
433 });
434}
435
436// Simple function name mangler.
437// This function simply takes LLVM's string representation of parameter types
438// and concatenate them with '_'. There are non-alphanumeric characters but llc
439// is ok with it, and we need to postprocess these names after the lowering
440// phase anyway.
441static std::string getSignature(FunctionType *FTy) {
442 std::string Sig;
443 raw_string_ostream OS(Sig);
444 OS << *FTy->getReturnType();
445 for (Type *ParamTy : FTy->params())
446 OS << "_" << *ParamTy;
447 if (FTy->isVarArg())
448 OS << "_...";
449 Sig = OS.str();
450 erase_if(C&: Sig, P: isSpace);
451 // When s2wasm parses .s file, a comma means the end of an argument. So a
452 // mangled function name can contain any character but a comma.
453 llvm::replace(Range&: Sig, OldValue: ',', NewValue: '.');
454 return Sig;
455}
456
457static Function *getFunction(FunctionType *Ty, const Twine &Name, Module *M) {
458 return Function::Create(Ty, Linkage: GlobalValue::ExternalLinkage, N: Name, M);
459}
460
461static void markAsImported(Function *F) {
462 // Tell the linker that this function is expected to be imported from the
463 // 'env' module. This is necessary for functions that do not have fixed names
464 // (e.g. __import_xyz). These names cannot be provided by any kind of shared
465 // or static library as instead we mark them explicitly as imported.
466 if (!F->hasFnAttribute(Kind: "wasm-import-module")) {
467 llvm::AttrBuilder B(F->getParent()->getContext());
468 B.addAttribute(A: "wasm-import-module", V: "env");
469 F->addFnAttrs(Attrs: B);
470 }
471 if (!F->hasFnAttribute(Kind: "wasm-import-name")) {
472 llvm::AttrBuilder B(F->getParent()->getContext());
473 B.addAttribute(A: "wasm-import-name", V: F->getName());
474 F->addFnAttrs(Attrs: B);
475 }
476}
477
478// Returns an integer type for the target architecture's address space.
479// i32 for wasm32 and i64 for wasm64.
480static Type *getAddrIntType(Module *M) {
481 IRBuilder<> IRB(M->getContext());
482 return IRB.getIntNTy(N: M->getDataLayout().getPointerSizeInBits());
483}
484
485// Returns an integer pointer type for the target architecture's address space.
486// i32* for wasm32 and i64* for wasm64. With opaque pointers this is just a ptr
487// in address space zero.
488static Type *getAddrPtrType(Module *M) {
489 return PointerType::getUnqual(C&: M->getContext());
490}
491
492// Returns an integer whose type is the integer type for the target's address
493// space. Returns (i32 C) for wasm32 and (i64 C) for wasm64, when C is the
494// integer.
495static Value *getAddrSizeInt(Module *M, uint64_t C) {
496 IRBuilder<> IRB(M->getContext());
497 return IRB.getIntN(N: M->getDataLayout().getPointerSizeInBits(), C);
498}
499
500// Returns true if the function has "target-features"="+exception-handling"
501// attribute.
502static bool hasEHTargetFeatureAttr(const Function &F) {
503 Attribute FeaturesAttr = F.getFnAttribute(Kind: "target-features");
504 return FeaturesAttr.isValid() &&
505 FeaturesAttr.getValueAsString().contains(Other: "+exception-handling");
506}
507
508// Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2.
509// This is because a landingpad instruction contains two more arguments, a
510// personality function and a cleanup bit, and __cxa_find_matching_catch_N
511// functions are named after the number of arguments in the original landingpad
512// instruction.
513Function *WebAssemblyLowerEmscriptenEHSjLjImpl::getFindMatchingCatch(
514 Module &M, unsigned NumClauses) {
515 auto [It, Inserted] = FindMatchingCatches.try_emplace(Key: NumClauses);
516 if (!Inserted)
517 return It->second;
518 PointerType *Int8PtrTy = PointerType::getUnqual(C&: M.getContext());
519 SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy);
520 FunctionType *FTy = FunctionType::get(Result: Int8PtrTy, Params: Args, isVarArg: false);
521 Function *F = getFunction(
522 Ty: FTy, Name: "__cxa_find_matching_catch_" + Twine(NumClauses + 2), M: &M);
523 markAsImported(F);
524 It->second = F;
525 return F;
526}
527
528// Generate invoke wrapper sequence with preamble and postamble
529// Preamble:
530// __THREW__ = 0;
531// Postamble:
532// %__THREW__.val = __THREW__; __THREW__ = 0;
533// Returns %__THREW__.val, which indicates whether an exception is thrown (or
534// whether longjmp occurred), for future use.
535Value *WebAssemblyLowerEmscriptenEHSjLjImpl::wrapInvoke(CallBase *CI) {
536 Module *M = CI->getModule();
537 LLVMContext &C = M->getContext();
538
539 IRBuilder<> IRB(C);
540 IRB.SetInsertPoint(CI);
541
542 // Pre-invoke
543 // __THREW__ = 0;
544 IRB.CreateStore(Val: getAddrSizeInt(M, C: 0), Ptr: ThrewGV);
545
546 // Invoke function wrapper in JavaScript
547 SmallVector<Value *, 16> Args;
548 // Put the pointer to the callee as first argument, so it can be called
549 // within the invoke wrapper later
550 Args.push_back(Elt: CI->getCalledOperand());
551 Args.append(in_start: CI->arg_begin(), in_end: CI->arg_end());
552 CallInst *NewCall = IRB.CreateCall(Callee: getInvokeWrapper(CI), Args);
553 NewCall->takeName(V: CI);
554 NewCall->setCallingConv(CallingConv::WASM_EmscriptenInvoke);
555 NewCall->setDebugLoc(CI->getDebugLoc());
556
557 // Because we added the pointer to the callee as first argument, all
558 // argument attribute indices have to be incremented by one.
559 SmallVector<AttributeSet, 8> ArgAttributes;
560 const AttributeList &InvokeAL = CI->getAttributes();
561
562 // No attributes for the callee pointer.
563 ArgAttributes.push_back(Elt: AttributeSet());
564 // Copy the argument attributes from the original
565 for (unsigned I = 0, E = CI->arg_size(); I < E; ++I)
566 ArgAttributes.push_back(Elt: InvokeAL.getParamAttrs(ArgNo: I));
567
568 AttrBuilder FnAttrs(CI->getContext(), InvokeAL.getFnAttrs());
569 if (auto Args = FnAttrs.getAllocSizeArgs()) {
570 // The allocsize attribute (if any) refers to parameters by index and needs
571 // to be adjusted.
572 auto [SizeArg, NEltArg] = *Args;
573 SizeArg += 1;
574 if (NEltArg)
575 NEltArg = *NEltArg + 1;
576 FnAttrs.addAllocSizeAttr(ElemSizeArg: SizeArg, NumElemsArg: NEltArg);
577 }
578 // In case the callee has 'noreturn' attribute, We need to remove it, because
579 // we expect invoke wrappers to return.
580 FnAttrs.removeAttribute(Val: Attribute::NoReturn);
581
582 // Reconstruct the AttributesList based on the vector we constructed.
583 AttributeList NewCallAL = AttributeList::get(
584 C, FnAttrs: AttributeSet::get(C, B: FnAttrs), RetAttrs: InvokeAL.getRetAttrs(), ArgAttrs: ArgAttributes);
585 NewCall->setAttributes(NewCallAL);
586
587 CI->replaceAllUsesWith(V: NewCall);
588
589 // Post-invoke
590 // %__THREW__.val = __THREW__; __THREW__ = 0;
591 Value *Threw =
592 IRB.CreateLoad(Ty: getAddrIntType(M), Ptr: ThrewGV, Name: ThrewGV->getName() + ".val");
593 IRB.CreateStore(Val: getAddrSizeInt(M, C: 0), Ptr: ThrewGV);
594 return Threw;
595}
596
597// Get matching invoke wrapper based on callee signature
598Function *WebAssemblyLowerEmscriptenEHSjLjImpl::getInvokeWrapper(CallBase *CI) {
599 Module *M = CI->getModule();
600 SmallVector<Type *, 16> ArgTys;
601 FunctionType *CalleeFTy = CI->getFunctionType();
602
603 std::string Sig = getSignature(FTy: CalleeFTy);
604 auto It = InvokeWrappers.find(Key: Sig);
605 if (It != InvokeWrappers.end())
606 return It->second;
607
608 // Put the pointer to the callee as first argument
609 ArgTys.push_back(Elt: PointerType::getUnqual(C&: CI->getContext()));
610 // Add argument types
611 ArgTys.append(in_start: CalleeFTy->param_begin(), in_end: CalleeFTy->param_end());
612
613 FunctionType *FTy = FunctionType::get(Result: CalleeFTy->getReturnType(), Params: ArgTys,
614 isVarArg: CalleeFTy->isVarArg());
615 Function *F = getFunction(Ty: FTy, Name: "__invoke_" + Sig, M);
616 markAsImported(F);
617 InvokeWrappers[Sig] = F;
618 return F;
619}
620
621static bool canLongjmp(const Value *Callee) {
622 if (auto *CalleeF = dyn_cast<Function>(Val: Callee))
623 if (CalleeF->isIntrinsic())
624 return false;
625
626 // Attempting to transform inline assembly will result in something like:
627 // call void @__invoke_void(void ()* asm ...)
628 // which is invalid because inline assembly blocks do not have addresses
629 // and can't be passed by pointer. The result is a crash with illegal IR.
630 if (isa<InlineAsm>(Val: Callee))
631 return false;
632 StringRef CalleeName = Callee->getName();
633
634 // TODO Include more functions or consider checking with mangled prefixes
635
636 // The reason we include malloc/free here is to exclude the malloc/free
637 // calls generated in setjmp prep / cleanup routines.
638 if (CalleeName == "setjmp" || CalleeName == "malloc" || CalleeName == "free")
639 return false;
640
641 // There are functions in Emscripten's JS glue code or compiler-rt
642 if (CalleeName == "__resumeException" || CalleeName == "llvm_eh_typeid_for" ||
643 CalleeName == "__wasm_setjmp" || CalleeName == "__wasm_setjmp_test" ||
644 CalleeName == "getTempRet0" || CalleeName == "setTempRet0")
645 return false;
646
647 // __cxa_find_matching_catch_N functions cannot longjmp
648 if (Callee->getName().starts_with(Prefix: "__cxa_find_matching_catch_"))
649 return false;
650
651 // Exception-catching related functions
652 //
653 // We intentionally treat __cxa_end_catch longjmpable in Wasm SjLj even though
654 // it surely cannot longjmp, in order to maintain the unwind relationship from
655 // all existing catchpads (and calls within them) to catch.dispatch.longjmp.
656 //
657 // In Wasm EH + Wasm SjLj, we
658 // 1. Make all catchswitch and cleanuppad that unwind to caller unwind to
659 // catch.dispatch.longjmp instead
660 // 2. Convert all longjmpable calls to invokes that unwind to
661 // catch.dispatch.longjmp
662 // But catchswitch BBs are removed in isel, so if an EH catchswitch (generated
663 // from an exception)'s catchpad does not contain any calls that are converted
664 // into invokes unwinding to catch.dispatch.longjmp, this unwind relationship
665 // (EH catchswitch BB -> catch.dispatch.longjmp BB) is lost and
666 // catch.dispatch.longjmp BB can be placed before the EH catchswitch BB in
667 // CFGSort.
668 // int ret = setjmp(buf);
669 // try {
670 // foo(); // longjmps
671 // } catch (...) {
672 // }
673 // Then in this code, if 'foo' longjmps, it first unwinds to 'catch (...)'
674 // catchswitch, and is not caught by that catchswitch because it is a longjmp,
675 // then it should next unwind to catch.dispatch.longjmp BB. But if this 'catch
676 // (...)' catchswitch -> catch.dispatch.longjmp unwind relationship is lost,
677 // it will not unwind to catch.dispatch.longjmp, producing an incorrect
678 // result.
679 //
680 // Every catchpad generated by Wasm C++ contains __cxa_end_catch, so we
681 // intentionally treat it as longjmpable to work around this problem. This is
682 // a hacky fix but an easy one.
683 if (CalleeName == "__cxa_end_catch")
684 return WebAssembly::WasmEnableSjLj;
685 if (CalleeName == "__cxa_begin_catch" ||
686 CalleeName == "__cxa_allocate_exception" || CalleeName == "__cxa_throw" ||
687 CalleeName == "__clang_call_terminate")
688 return false;
689
690 // std::terminate, which is generated when another exception occurs while
691 // handling an exception, cannot longjmp.
692 if (CalleeName == "_ZSt9terminatev")
693 return false;
694
695 // Otherwise we don't know
696 return true;
697}
698
699static bool isEmAsmCall(const Value *Callee) {
700 StringRef CalleeName = Callee->getName();
701 // This is an exhaustive list from Emscripten's <emscripten/em_asm.h>.
702 return CalleeName == "emscripten_asm_const_int" ||
703 CalleeName == "emscripten_asm_const_double" ||
704 CalleeName == "emscripten_asm_const_int_sync_on_main_thread" ||
705 CalleeName == "emscripten_asm_const_double_sync_on_main_thread" ||
706 CalleeName == "emscripten_asm_const_async_on_main_thread";
707}
708
709// Generate __wasm_setjmp_test function call sequence with preamble and
710// postamble. The code this generates is equivalent to the following
711// JavaScript code:
712// %__threwValue.val = __threwValue;
713// if (%__THREW__.val != 0 & %__threwValue.val != 0) {
714// %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
715// if (%label == 0)
716// emscripten_longjmp(%__THREW__.val, %__threwValue.val);
717// setTempRet0(%__threwValue.val);
718// } else {
719// %label = -1;
720// }
721// %longjmp_result = getTempRet0();
722//
723// As output parameters. returns %label, %longjmp_result, and the BB the last
724// instruction (%longjmp_result = ...) is in.
725void WebAssemblyLowerEmscriptenEHSjLjImpl::wrapTestSetjmp(
726 BasicBlock *BB, DebugLoc DL, Value *Threw, Value *FunctionInvocationId,
727 Value *&Label, Value *&LongjmpResult, BasicBlock *&CallEmLongjmpBB,
728 PHINode *&CallEmLongjmpBBThrewPHI, PHINode *&CallEmLongjmpBBThrewValuePHI,
729 BasicBlock *&EndBB) {
730 Function *F = BB->getParent();
731 Module *M = F->getParent();
732 LLVMContext &C = M->getContext();
733 IRBuilder<> IRB(C);
734 IRB.SetCurrentDebugLocation(DL);
735
736 // if (%__THREW__.val != 0 & %__threwValue.val != 0)
737 IRB.SetInsertPoint(BB);
738 BasicBlock *ThenBB1 = BasicBlock::Create(Context&: C, Name: "if.then1", Parent: F);
739 BasicBlock *ElseBB1 = BasicBlock::Create(Context&: C, Name: "if.else1", Parent: F);
740 BasicBlock *EndBB1 = BasicBlock::Create(Context&: C, Name: "if.end", Parent: F);
741 Value *ThrewCmp = IRB.CreateICmpNE(LHS: Threw, RHS: getAddrSizeInt(M, C: 0));
742 Value *ThrewValue = IRB.CreateLoad(Ty: IRB.getInt32Ty(), Ptr: ThrewValueGV,
743 Name: ThrewValueGV->getName() + ".val");
744 Value *ThrewValueCmp = IRB.CreateICmpNE(LHS: ThrewValue, RHS: IRB.getInt32(C: 0));
745 Value *Cmp1 = IRB.CreateAnd(LHS: ThrewCmp, RHS: ThrewValueCmp, Name: "cmp1");
746 IRB.CreateCondBr(Cond: Cmp1, True: ThenBB1, False: ElseBB1);
747
748 // Generate call.em.longjmp BB once and share it within the function
749 if (!CallEmLongjmpBB) {
750 // emscripten_longjmp(%__THREW__.val, %__threwValue.val);
751 CallEmLongjmpBB = BasicBlock::Create(Context&: C, Name: "call.em.longjmp", Parent: F);
752 IRB.SetInsertPoint(CallEmLongjmpBB);
753 CallEmLongjmpBBThrewPHI = IRB.CreatePHI(Ty: getAddrIntType(M), NumReservedValues: 4, Name: "threw.phi");
754 CallEmLongjmpBBThrewValuePHI =
755 IRB.CreatePHI(Ty: IRB.getInt32Ty(), NumReservedValues: 4, Name: "threwvalue.phi");
756 CallEmLongjmpBBThrewPHI->addIncoming(V: Threw, BB: ThenBB1);
757 CallEmLongjmpBBThrewValuePHI->addIncoming(V: ThrewValue, BB: ThenBB1);
758 IRB.CreateCall(Callee: EmLongjmpF,
759 Args: {CallEmLongjmpBBThrewPHI, CallEmLongjmpBBThrewValuePHI});
760 IRB.CreateUnreachable();
761 } else {
762 CallEmLongjmpBBThrewPHI->addIncoming(V: Threw, BB: ThenBB1);
763 CallEmLongjmpBBThrewValuePHI->addIncoming(V: ThrewValue, BB: ThenBB1);
764 }
765
766 // %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
767 // if (%label == 0)
768 IRB.SetInsertPoint(ThenBB1);
769 BasicBlock *EndBB2 = BasicBlock::Create(Context&: C, Name: "if.end2", Parent: F);
770 Value *ThrewPtr =
771 IRB.CreateIntToPtr(V: Threw, DestTy: getAddrPtrType(M), Name: Threw->getName() + ".p");
772 Value *ThenLabel = IRB.CreateCall(Callee: WasmSetjmpTestF,
773 Args: {ThrewPtr, FunctionInvocationId}, Name: "label");
774 Value *Cmp2 = IRB.CreateICmpEQ(LHS: ThenLabel, RHS: IRB.getInt32(C: 0));
775 IRB.CreateCondBr(Cond: Cmp2, True: CallEmLongjmpBB, False: EndBB2);
776
777 // setTempRet0(%__threwValue.val);
778 IRB.SetInsertPoint(EndBB2);
779 IRB.CreateCall(Callee: SetTempRet0F, Args: ThrewValue);
780 IRB.CreateBr(Dest: EndBB1);
781
782 IRB.SetInsertPoint(ElseBB1);
783 IRB.CreateBr(Dest: EndBB1);
784
785 // longjmp_result = getTempRet0();
786 IRB.SetInsertPoint(EndBB1);
787 PHINode *LabelPHI = IRB.CreatePHI(Ty: IRB.getInt32Ty(), NumReservedValues: 2, Name: "label");
788 LabelPHI->addIncoming(V: ThenLabel, BB: EndBB2);
789
790 LabelPHI->addIncoming(V: IRB.getInt32(C: -1), BB: ElseBB1);
791
792 // Output parameter assignment
793 Label = LabelPHI;
794 EndBB = EndBB1;
795 LongjmpResult = IRB.CreateCall(Callee: GetTempRet0F, Args: {}, Name: "longjmp_result");
796}
797
798void WebAssemblyLowerEmscriptenEHSjLjImpl::rebuildSSA(Function &F) {
799 DominatorTree &DT = GetDominatorTree(F);
800 DT.recalculate(Func&: F); // CFG has been changed
801
802 SSAUpdaterBulk SSA;
803 for (BasicBlock &BB : F) {
804 for (Instruction &I : BB) {
805 if (I.getType()->isVoidTy())
806 continue;
807
808 if (isa<AllocaInst>(Val: &I)) {
809 // If the alloca has any lifetime marker that is no longer dominated
810 // by the alloca, remove all lifetime markers. Lifetime markers must
811 // always work directly on the alloca, and this is no longer possible.
812 bool HasNonDominatedLifetimeMarker = any_of(Range: I.users(), P: [&](User *U) {
813 auto *UserI = cast<Instruction>(Val: U);
814 return UserI->isLifetimeStartOrEnd() && !DT.dominates(Def: &I, User: UserI);
815 });
816 if (HasNonDominatedLifetimeMarker) {
817 for (User *U : make_early_inc_range(Range: I.users())) {
818 auto *UserI = cast<Instruction>(Val: U);
819 if (UserI->isLifetimeStartOrEnd())
820 UserI->eraseFromParent();
821 }
822 }
823 }
824
825 unsigned VarID = SSA.AddVariable(Name: I.getName(), Ty: I.getType());
826 // If a value is defined by an invoke instruction, it is only available in
827 // its normal destination and not in its unwind destination.
828 if (auto *II = dyn_cast<InvokeInst>(Val: &I))
829 SSA.AddAvailableValue(Var: VarID, BB: II->getNormalDest(), V: II);
830 else
831 SSA.AddAvailableValue(Var: VarID, BB: &BB, V: &I);
832 for (auto &U : I.uses()) {
833 auto *User = cast<Instruction>(Val: U.getUser());
834 if (auto *UserPN = dyn_cast<PHINode>(Val: User))
835 if (UserPN->getIncomingBlock(U) == &BB)
836 continue;
837 if (DT.dominates(Def: &I, User))
838 continue;
839 SSA.AddUse(Var: VarID, U: &U);
840 }
841 }
842 }
843 SSA.RewriteAllUses(DT: &DT);
844}
845
846// Replace uses of longjmp with a new longjmp function in Emscripten library.
847// In Emscripten SjLj, the new function is
848// void emscripten_longjmp(uintptr_t, i32)
849// In Wasm SjLj, the new function is
850// void __wasm_longjmp(i8*, i32)
851// Because the original libc longjmp function takes (jmp_buf*, i32), we need a
852// ptrtoint/bitcast instruction here to make the type match. jmp_buf* will
853// eventually be lowered to i32/i64 in the wasm backend.
854void WebAssemblyLowerEmscriptenEHSjLjImpl::replaceLongjmpWith(
855 Function *LongjmpF, Function *NewF) {
856 assert(NewF == EmLongjmpF || NewF == WasmLongjmpF);
857 Module *M = LongjmpF->getParent();
858 SmallVector<CallInst *, 8> ToErase;
859 LLVMContext &C = LongjmpF->getParent()->getContext();
860 IRBuilder<> IRB(C);
861
862 // For calls to longjmp, replace it with emscripten_longjmp/__wasm_longjmp and
863 // cast its first argument (jmp_buf*) appropriately
864 for (User *U : LongjmpF->users()) {
865 auto *CI = dyn_cast<CallInst>(Val: U);
866 if (CI && CI->getCalledFunction() == LongjmpF) {
867 IRB.SetInsertPoint(CI);
868 Value *Env = nullptr;
869 if (NewF == EmLongjmpF)
870 Env =
871 IRB.CreatePtrToInt(V: CI->getArgOperand(i: 0), DestTy: getAddrIntType(M), Name: "env");
872 else // WasmLongjmpF
873 Env = IRB.CreateBitCast(V: CI->getArgOperand(i: 0), DestTy: IRB.getPtrTy(), Name: "env");
874 IRB.CreateCall(Callee: NewF, Args: {Env, CI->getArgOperand(i: 1)});
875 ToErase.push_back(Elt: CI);
876 }
877 }
878 for (auto *I : ToErase)
879 I->eraseFromParent();
880
881 // If we have any remaining uses of longjmp's function pointer, replace it
882 // with (void(*)(jmp_buf*, int))emscripten_longjmp / __wasm_longjmp.
883 if (!LongjmpF->uses().empty()) {
884 Value *NewLongjmp =
885 IRB.CreateBitCast(V: NewF, DestTy: LongjmpF->getType(), Name: "longjmp.cast");
886 LongjmpF->replaceAllUsesWith(V: NewLongjmp);
887 }
888}
889
890static bool containsLongjmpableCalls(const Function *F) {
891 for (const auto &BB : *F)
892 for (const auto &I : BB)
893 if (const auto *CB = dyn_cast<CallBase>(Val: &I))
894 if (canLongjmp(Callee: CB->getCalledOperand()))
895 return true;
896 return false;
897}
898
899// When a function contains a setjmp call but not other calls that can longjmp,
900// we don't do setjmp transformation for that setjmp. But we need to convert the
901// setjmp calls into "i32 0" so they don't cause link time errors. setjmp always
902// returns 0 when called directly.
903static void nullifySetjmp(Function *F) {
904 Module &M = *F->getParent();
905 IRBuilder<> IRB(M.getContext());
906 Function *SetjmpF = M.getFunction(Name: "setjmp");
907 SmallVector<Instruction *, 1> ToErase;
908
909 for (User *U : make_early_inc_range(Range: SetjmpF->users())) {
910 auto *CB = cast<CallBase>(Val: U);
911 BasicBlock *BB = CB->getParent();
912 if (BB->getParent() != F) // in other function
913 continue;
914 CallInst *CI = nullptr;
915 // setjmp cannot throw. So if it is an invoke, lower it to a call
916 if (auto *II = dyn_cast<InvokeInst>(Val: CB))
917 CI = llvm::changeToCall(II);
918 else
919 CI = cast<CallInst>(Val: CB);
920 ToErase.push_back(Elt: CI);
921 CI->replaceAllUsesWith(V: IRB.getInt32(C: 0));
922 }
923 for (auto *I : ToErase)
924 I->eraseFromParent();
925}
926
927bool WebAssemblyLowerEmscriptenEHSjLjImpl::runOnModule(Module &M) {
928 LLVM_DEBUG(dbgs() << "********** Lower Emscripten EH & SjLj **********\n");
929
930 // The Emscripten EH model may come from the "exception-model" module flag
931 // (e.g. when this pass is run standalone via opt) in addition to being
932 // threaded in from the TargetMachine.
933 EnableEmEH |= M.getExceptionModel() == ExceptionHandling::Emscripten;
934
935 LLVMContext &C = M.getContext();
936 IRBuilder<> IRB(C);
937
938 Function *SetjmpF = M.getFunction(Name: "setjmp");
939 Function *LongjmpF = M.getFunction(Name: "longjmp");
940
941 // In some platforms _setjmp and _longjmp are used instead. Change these to
942 // use setjmp/longjmp instead, because we later detect these functions by
943 // their names.
944 Function *SetjmpF2 = M.getFunction(Name: "_setjmp");
945 Function *LongjmpF2 = M.getFunction(Name: "_longjmp");
946 if (SetjmpF2) {
947 if (SetjmpF) {
948 if (SetjmpF->getFunctionType() != SetjmpF2->getFunctionType())
949 report_fatal_error(reason: "setjmp and _setjmp have different function types");
950 } else {
951 SetjmpF = Function::Create(Ty: SetjmpF2->getFunctionType(),
952 Linkage: GlobalValue::ExternalLinkage, N: "setjmp", M);
953 }
954 SetjmpF2->replaceAllUsesWith(V: SetjmpF);
955 }
956 if (LongjmpF2) {
957 if (LongjmpF) {
958 if (LongjmpF->getFunctionType() != LongjmpF2->getFunctionType())
959 report_fatal_error(
960 reason: "longjmp and _longjmp have different function types");
961 } else {
962 LongjmpF = Function::Create(Ty: LongjmpF2->getFunctionType(),
963 Linkage: GlobalValue::ExternalLinkage, N: "setjmp", M);
964 }
965 LongjmpF2->replaceAllUsesWith(V: LongjmpF);
966 }
967
968 // Declare (or get) global variables __THREW__, __threwValue, and
969 // getTempRet0/setTempRet0 function which are used in common for both
970 // exception handling and setjmp/longjmp handling
971 ThrewGV = getGlobalVariable(M, Ty: getAddrIntType(M: &M), Name: "__THREW__");
972 ThrewValueGV = getGlobalVariable(M, Ty: IRB.getInt32Ty(), Name: "__threwValue");
973 GetTempRet0F = getFunction(Ty: FunctionType::get(Result: IRB.getInt32Ty(), isVarArg: false),
974 Name: "getTempRet0", M: &M);
975 SetTempRet0F =
976 getFunction(Ty: FunctionType::get(Result: IRB.getVoidTy(), Params: IRB.getInt32Ty(), isVarArg: false),
977 Name: "setTempRet0", M: &M);
978 GetTempRet0F->setDoesNotThrow();
979 SetTempRet0F->setDoesNotThrow();
980
981 bool Changed = false;
982
983 // Function registration for exception handling
984 if (EnableEmEH) {
985 // Register __resumeException function
986 FunctionType *ResumeFTy =
987 FunctionType::get(Result: IRB.getVoidTy(), Params: IRB.getPtrTy(), isVarArg: false);
988 ResumeF = getFunction(Ty: ResumeFTy, Name: "__resumeException", M: &M);
989 ResumeF->addFnAttr(Kind: Attribute::NoReturn);
990
991 // Register llvm_eh_typeid_for function
992 FunctionType *EHTypeIDTy =
993 FunctionType::get(Result: IRB.getInt32Ty(), Params: IRB.getPtrTy(), isVarArg: false);
994 EHTypeIDF = getFunction(Ty: EHTypeIDTy, Name: "llvm_eh_typeid_for", M: &M);
995 }
996
997 // Functions that contains calls to setjmp but don't have other longjmpable
998 // calls within them.
999 SmallPtrSet<Function *, 4> SetjmpUsersToNullify;
1000
1001 if ((EnableEmSjLj || EnableWasmSjLj) && SetjmpF) {
1002 // Precompute setjmp users
1003 for (User *U : SetjmpF->users()) {
1004 if (auto *CB = dyn_cast<CallBase>(Val: U)) {
1005 auto *UserF = CB->getFunction();
1006 // If a function that calls setjmp does not contain any other calls that
1007 // can longjmp, we don't need to do any transformation on that function,
1008 // so can ignore it
1009 if (containsLongjmpableCalls(F: UserF))
1010 SetjmpUsers.insert(Ptr: UserF);
1011 else
1012 SetjmpUsersToNullify.insert(Ptr: UserF);
1013 } else {
1014 std::string S;
1015 raw_string_ostream SS(S);
1016 SS << *U;
1017 report_fatal_error(reason: Twine("Indirect use of setjmp is not supported: ") +
1018 SS.str());
1019 }
1020 }
1021 }
1022
1023 bool SetjmpUsed = SetjmpF && !SetjmpUsers.empty();
1024 bool LongjmpUsed = LongjmpF && !LongjmpF->use_empty();
1025 DoSjLj = (EnableEmSjLj | EnableWasmSjLj) && (SetjmpUsed || LongjmpUsed);
1026
1027 // Function registration and data pre-gathering for setjmp/longjmp handling
1028 if (DoSjLj) {
1029 assert(EnableEmSjLj || EnableWasmSjLj);
1030
1031 if (EnableEmSjLj) {
1032 // Register emscripten_longjmp function
1033 FunctionType *FTy = FunctionType::get(
1034 Result: IRB.getVoidTy(), Params: {getAddrIntType(M: &M), IRB.getInt32Ty()}, isVarArg: false);
1035 EmLongjmpF = getFunction(Ty: FTy, Name: "emscripten_longjmp", M: &M);
1036 EmLongjmpF->addFnAttr(Kind: Attribute::NoReturn);
1037 } else { // EnableWasmSjLj
1038 Type *Int8PtrTy = IRB.getPtrTy();
1039 // Register __wasm_longjmp function, which calls __builtin_wasm_longjmp.
1040 FunctionType *FTy = FunctionType::get(
1041 Result: IRB.getVoidTy(), Params: {Int8PtrTy, IRB.getInt32Ty()}, isVarArg: false);
1042 WasmLongjmpF = getFunction(Ty: FTy, Name: "__wasm_longjmp", M: &M);
1043 WasmLongjmpF->addFnAttr(Kind: Attribute::NoReturn);
1044 }
1045
1046 if (EnableWasmSjLj) {
1047 for (auto *SjLjF : {SetjmpF, LongjmpF}) {
1048 if (SjLjF) {
1049 for (User *U : SjLjF->users()) {
1050 if (auto *CI = dyn_cast<CallInst>(Val: U)) {
1051 auto &F = *CI->getFunction();
1052 if (!hasEHTargetFeatureAttr(F))
1053 report_fatal_error(reason: "Function " + F.getName() +
1054 " is using setjmp/longjmp but does not have "
1055 "+exception-handling target feature");
1056 }
1057 }
1058 }
1059 }
1060 }
1061
1062 if (SetjmpF) {
1063 Type *Int8PtrTy = IRB.getPtrTy();
1064 Type *Int32PtrTy = IRB.getPtrTy();
1065 Type *Int32Ty = IRB.getInt32Ty();
1066
1067 // Register __wasm_setjmp function
1068 FunctionType *SetjmpFTy = SetjmpF->getFunctionType();
1069 FunctionType *FTy = FunctionType::get(
1070 Result: IRB.getVoidTy(), Params: {SetjmpFTy->getParamType(i: 0), Int32Ty, Int32PtrTy},
1071 isVarArg: false);
1072 WasmSetjmpF = getFunction(Ty: FTy, Name: "__wasm_setjmp", M: &M);
1073
1074 // Register __wasm_setjmp_test function
1075 FTy = FunctionType::get(Result: Int32Ty, Params: {Int32PtrTy, Int32PtrTy}, isVarArg: false);
1076 WasmSetjmpTestF = getFunction(Ty: FTy, Name: "__wasm_setjmp_test", M: &M);
1077
1078 // wasm.catch() will be lowered down to wasm 'catch' instruction in
1079 // instruction selection.
1080 CatchF = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::wasm_catch);
1081 // Type for struct __WasmLongjmpArgs
1082 LongjmpArgsTy = StructType::get(elt1: Int8PtrTy, // env
1083 elts: Int32Ty // val
1084 );
1085 }
1086 }
1087
1088 // Exception handling transformation
1089 if (EnableEmEH) {
1090 for (Function &F : M) {
1091 if (F.isDeclaration())
1092 continue;
1093 Changed |= runEHOnFunction(F);
1094 }
1095 }
1096
1097 // Setjmp/longjmp handling transformation
1098 if (DoSjLj) {
1099 Changed = true; // We have setjmp or longjmp somewhere
1100 if (LongjmpF)
1101 replaceLongjmpWith(LongjmpF, NewF: EnableEmSjLj ? EmLongjmpF : WasmLongjmpF);
1102 // Only traverse functions that uses setjmp in order not to insert
1103 // unnecessary prep / cleanup code in every function
1104 if (SetjmpF)
1105 for (Function *F : SetjmpUsers)
1106 runSjLjOnFunction(F&: *F);
1107 }
1108
1109 // Replace unnecessary setjmp calls with 0
1110 if ((EnableEmSjLj || EnableWasmSjLj) && !SetjmpUsersToNullify.empty()) {
1111 Changed = true;
1112 assert(SetjmpF);
1113 for (Function *F : SetjmpUsersToNullify)
1114 nullifySetjmp(F);
1115 }
1116
1117 // Delete unused global variables and functions
1118 for (auto *V : {ThrewGV, ThrewValueGV})
1119 if (V && V->use_empty())
1120 V->eraseFromParent();
1121 for (auto *V : {GetTempRet0F, SetTempRet0F, ResumeF, EHTypeIDF, EmLongjmpF,
1122 WasmSetjmpF, WasmSetjmpTestF, WasmLongjmpF, CatchF})
1123 if (V && V->use_empty())
1124 V->eraseFromParent();
1125
1126 return Changed;
1127}
1128
1129bool WebAssemblyLowerEmscriptenEHSjLjImpl::runEHOnFunction(Function &F) {
1130 Module &M = *F.getParent();
1131 LLVMContext &C = F.getContext();
1132 IRBuilder<> IRB(C);
1133 bool Changed = false;
1134 SmallVector<Instruction *, 64> ToErase;
1135 SmallPtrSet<LandingPadInst *, 32> LandingPads;
1136
1137 // rethrow.longjmp BB that will be shared within the function.
1138 BasicBlock *RethrowLongjmpBB = nullptr;
1139 // PHI node for the loaded value of __THREW__ global variable in
1140 // rethrow.longjmp BB
1141 PHINode *RethrowLongjmpBBThrewPHI = nullptr;
1142
1143 for (BasicBlock &BB : F) {
1144 auto *II = dyn_cast<InvokeInst>(Val: BB.getTerminator());
1145 if (!II)
1146 continue;
1147 Changed = true;
1148 LandingPads.insert(Ptr: II->getLandingPadInst());
1149 IRB.SetInsertPoint(II);
1150
1151 const Value *Callee = II->getCalledOperand();
1152 bool NeedInvoke = supportsException(F: &F) && canThrow(V: Callee);
1153 if (NeedInvoke) {
1154 // Wrap invoke with invoke wrapper and generate preamble/postamble
1155 Value *Threw = wrapInvoke(CI: II);
1156 ToErase.push_back(Elt: II);
1157
1158 // If setjmp/longjmp handling is enabled, the thrown value can be not an
1159 // exception but a longjmp. If the current function contains calls to
1160 // setjmp, it will be appropriately handled in runSjLjOnFunction. But even
1161 // if the function does not contain setjmp calls, we shouldn't silently
1162 // ignore longjmps; we should rethrow them so they can be correctly
1163 // handled in somewhere up the call chain where setjmp is. __THREW__'s
1164 // value is 0 when nothing happened, 1 when an exception is thrown, and
1165 // other values when longjmp is thrown.
1166 //
1167 // if (%__THREW__.val == 0 || %__THREW__.val == 1)
1168 // goto %tail
1169 // else
1170 // goto %longjmp.rethrow
1171 //
1172 // rethrow.longjmp: ;; This is longjmp. Rethrow it
1173 // %__threwValue.val = __threwValue
1174 // emscripten_longjmp(%__THREW__.val, %__threwValue.val);
1175 //
1176 // tail: ;; Nothing happened or an exception is thrown
1177 // ... Continue exception handling ...
1178 if (DoSjLj && EnableEmSjLj && !SetjmpUsers.count(Ptr: &F) &&
1179 canLongjmp(Callee)) {
1180 // Create longjmp.rethrow BB once and share it within the function
1181 if (!RethrowLongjmpBB) {
1182 RethrowLongjmpBB = BasicBlock::Create(Context&: C, Name: "rethrow.longjmp", Parent: &F);
1183 IRB.SetInsertPoint(RethrowLongjmpBB);
1184 RethrowLongjmpBBThrewPHI =
1185 IRB.CreatePHI(Ty: getAddrIntType(M: &M), NumReservedValues: 4, Name: "threw.phi");
1186 RethrowLongjmpBBThrewPHI->addIncoming(V: Threw, BB: &BB);
1187 Value *ThrewValue = IRB.CreateLoad(Ty: IRB.getInt32Ty(), Ptr: ThrewValueGV,
1188 Name: ThrewValueGV->getName() + ".val");
1189 IRB.CreateCall(Callee: EmLongjmpF, Args: {RethrowLongjmpBBThrewPHI, ThrewValue});
1190 IRB.CreateUnreachable();
1191 } else {
1192 RethrowLongjmpBBThrewPHI->addIncoming(V: Threw, BB: &BB);
1193 }
1194
1195 IRB.SetInsertPoint(II); // Restore the insert point back
1196 BasicBlock *Tail = BasicBlock::Create(Context&: C, Name: "tail", Parent: &F);
1197 Value *CmpEqOne =
1198 IRB.CreateICmpEQ(LHS: Threw, RHS: getAddrSizeInt(M: &M, C: 1), Name: "cmp.eq.one");
1199 Value *CmpEqZero =
1200 IRB.CreateICmpEQ(LHS: Threw, RHS: getAddrSizeInt(M: &M, C: 0), Name: "cmp.eq.zero");
1201 Value *Or = IRB.CreateOr(LHS: CmpEqZero, RHS: CmpEqOne, Name: "or");
1202 IRB.CreateCondBr(Cond: Or, True: Tail, False: RethrowLongjmpBB);
1203 IRB.SetInsertPoint(Tail);
1204 BB.replaceSuccessorsPhiUsesWith(Old: &BB, New: Tail);
1205 }
1206
1207 // Insert a branch based on __THREW__ variable
1208 Value *Cmp = IRB.CreateICmpEQ(LHS: Threw, RHS: getAddrSizeInt(M: &M, C: 1), Name: "cmp");
1209 IRB.CreateCondBr(Cond: Cmp, True: II->getUnwindDest(), False: II->getNormalDest());
1210
1211 } else {
1212 // This can't throw, and we don't need this invoke, just replace it with a
1213 // call+branch
1214 changeToCall(II);
1215 }
1216 }
1217
1218 // Process resume instructions
1219 for (BasicBlock &BB : F) {
1220 // Scan the body of the basic block for resumes
1221 for (Instruction &I : BB) {
1222 auto *RI = dyn_cast<ResumeInst>(Val: &I);
1223 if (!RI)
1224 continue;
1225 Changed = true;
1226
1227 // Split the input into legal values
1228 Value *Input = RI->getValue();
1229 IRB.SetInsertPoint(RI);
1230 Value *Low = IRB.CreateExtractValue(Agg: Input, Idxs: 0, Name: "low");
1231 // Create a call to __resumeException function
1232 IRB.CreateCall(Callee: ResumeF, Args: {Low});
1233 // Add a terminator to the block
1234 IRB.CreateUnreachable();
1235 ToErase.push_back(Elt: RI);
1236 }
1237 }
1238
1239 // Process llvm.eh.typeid.for intrinsics
1240 for (BasicBlock &BB : F) {
1241 for (Instruction &I : BB) {
1242 auto *CI = dyn_cast<CallInst>(Val: &I);
1243 if (!CI)
1244 continue;
1245 const Function *Callee = CI->getCalledFunction();
1246 if (!Callee)
1247 continue;
1248 if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for)
1249 continue;
1250 Changed = true;
1251
1252 IRB.SetInsertPoint(CI);
1253 CallInst *NewCI =
1254 IRB.CreateCall(Callee: EHTypeIDF, Args: CI->getArgOperand(i: 0), Name: "typeid");
1255 CI->replaceAllUsesWith(V: NewCI);
1256 ToErase.push_back(Elt: CI);
1257 }
1258 }
1259
1260 // Look for orphan landingpads, can occur in blocks with no predecessors
1261 for (BasicBlock &BB : F) {
1262 BasicBlock::iterator I = BB.getFirstNonPHIIt();
1263 if (auto *LPI = dyn_cast<LandingPadInst>(Val&: I))
1264 LandingPads.insert(Ptr: LPI);
1265 }
1266 Changed |= !LandingPads.empty();
1267
1268 // Handle all the landingpad for this function together, as multiple invokes
1269 // may share a single lp
1270 for (LandingPadInst *LPI : LandingPads) {
1271 IRB.SetInsertPoint(LPI);
1272 SmallVector<Value *, 16> FMCArgs;
1273 for (unsigned I = 0, E = LPI->getNumClauses(); I < E; ++I) {
1274 Constant *Clause = LPI->getClause(Idx: I);
1275 // TODO Handle filters (= exception specifications).
1276 // https://github.com/llvm/llvm-project/issues/49740
1277 if (LPI->isCatch(Idx: I))
1278 FMCArgs.push_back(Elt: Clause);
1279 }
1280
1281 // Create a call to __cxa_find_matching_catch_N function
1282 Function *FMCF = getFindMatchingCatch(M, NumClauses: FMCArgs.size());
1283 CallInst *FMCI = IRB.CreateCall(Callee: FMCF, Args: FMCArgs, Name: "fmc");
1284 Value *Poison = PoisonValue::get(T: LPI->getType());
1285 Value *Pair0 = IRB.CreateInsertValue(Agg: Poison, Val: FMCI, Idxs: 0, Name: "pair0");
1286 Value *TempRet0 = IRB.CreateCall(Callee: GetTempRet0F, Args: {}, Name: "tempret0");
1287 Value *Pair1 = IRB.CreateInsertValue(Agg: Pair0, Val: TempRet0, Idxs: 1, Name: "pair1");
1288
1289 LPI->replaceAllUsesWith(V: Pair1);
1290 ToErase.push_back(Elt: LPI);
1291 }
1292
1293 // Erase everything we no longer need in this function
1294 for (Instruction *I : ToErase)
1295 I->eraseFromParent();
1296
1297 return Changed;
1298}
1299
1300// This tries to get debug info from the instruction before which a new
1301// instruction will be inserted, and if there's no debug info in that
1302// instruction, tries to get the info instead from the previous instruction (if
1303// any). If none of these has debug info and a DISubprogram is provided, it
1304// creates a dummy debug info with the first line of the function, because IR
1305// verifier requires all inlinable callsites should have debug info when both a
1306// caller and callee have DISubprogram. If none of these conditions are met,
1307// returns empty info.
1308static DebugLoc getOrCreateDebugLoc(const Instruction *InsertBefore,
1309 DISubprogram *SP) {
1310 assert(InsertBefore);
1311 if (InsertBefore->getDebugLoc())
1312 return InsertBefore->getDebugLoc();
1313 const Instruction *Prev = InsertBefore->getPrevNode();
1314 if (Prev && Prev->getDebugLoc())
1315 return Prev->getDebugLoc();
1316 if (SP)
1317 return DILocation::get(Context&: SP->getContext(), Line: SP->getLine(), Column: 1, Scope: SP);
1318 return DebugLoc();
1319}
1320
1321bool WebAssemblyLowerEmscriptenEHSjLjImpl::runSjLjOnFunction(Function &F) {
1322 assert(EnableEmSjLj || EnableWasmSjLj);
1323 Module &M = *F.getParent();
1324 LLVMContext &C = F.getContext();
1325 IRBuilder<> IRB(C);
1326 SmallVector<Instruction *, 64> ToErase;
1327
1328 // Setjmp preparation
1329
1330 SmallVector<AllocaInst *> StaticAllocas;
1331 for (Instruction &I : F.getEntryBlock())
1332 if (auto *AI = dyn_cast<AllocaInst>(Val: &I))
1333 if (AI->isStaticAlloca())
1334 StaticAllocas.push_back(Elt: AI);
1335
1336 BasicBlock *Entry = &F.getEntryBlock();
1337 DebugLoc FirstDL = getOrCreateDebugLoc(InsertBefore: &*Entry->begin(), SP: F.getSubprogram());
1338 SplitBlock(Old: Entry, SplitPt: &*Entry->getFirstInsertionPt());
1339
1340 // Move static allocas back into the entry block, so they stay static.
1341 for (AllocaInst *AI : StaticAllocas)
1342 AI->moveBefore(InsertPos: Entry->getTerminator()->getIterator());
1343
1344 IRB.SetInsertPoint(Entry->getTerminator()->getIterator());
1345 // This alloca'ed pointer is used by the runtime to identify function
1346 // invocations. It's just for pointer comparisons. It will never be
1347 // dereferenced.
1348 Instruction *FunctionInvocationId =
1349 IRB.CreateAlloca(Ty: IRB.getInt32Ty(), ArraySize: nullptr, Name: "functionInvocationId");
1350 FunctionInvocationId->setDebugLoc(FirstDL);
1351
1352 // Setjmp transformation
1353 SmallVector<PHINode *, 4> SetjmpRetPHIs;
1354 Function *SetjmpF = M.getFunction(Name: "setjmp");
1355 for (auto *U : make_early_inc_range(Range: SetjmpF->users())) {
1356 auto *CB = cast<CallBase>(Val: U);
1357 BasicBlock *BB = CB->getParent();
1358 if (BB->getParent() != &F) // in other function
1359 continue;
1360 if (CB->getOperandBundle(ID: LLVMContext::OB_funclet)) {
1361 std::string S;
1362 raw_string_ostream SS(S);
1363 SS << "In function " + F.getName() +
1364 ": setjmp within a catch clause is not supported in Wasm EH:\n";
1365 SS << *CB;
1366 report_fatal_error(reason: StringRef(SS.str()));
1367 }
1368
1369 CallInst *CI = nullptr;
1370 // setjmp cannot throw. So if it is an invoke, lower it to a call
1371 if (auto *II = dyn_cast<InvokeInst>(Val: CB))
1372 CI = llvm::changeToCall(II);
1373 else
1374 CI = cast<CallInst>(Val: CB);
1375
1376 // The tail is everything right after the call, and will be reached once
1377 // when setjmp is called, and later when longjmp returns to the setjmp
1378 BasicBlock *Tail = SplitBlock(Old: BB, SplitPt: CI->getNextNode());
1379 // Add a phi to the tail, which will be the output of setjmp, which
1380 // indicates if this is the first call or a longjmp back. The phi directly
1381 // uses the right value based on where we arrive from
1382 IRB.SetInsertPoint(TheBB: Tail, IP: Tail->getFirstNonPHIIt());
1383 PHINode *SetjmpRet = IRB.CreatePHI(Ty: IRB.getInt32Ty(), NumReservedValues: 2, Name: "setjmp.ret");
1384
1385 // setjmp initial call returns 0
1386 SetjmpRet->addIncoming(V: IRB.getInt32(C: 0), BB);
1387 // The proper output is now this, not the setjmp call itself
1388 CI->replaceAllUsesWith(V: SetjmpRet);
1389 // longjmp returns to the setjmp will add themselves to this phi
1390 SetjmpRetPHIs.push_back(Elt: SetjmpRet);
1391
1392 // Fix call target
1393 // Our index in the function is our place in the array + 1 to avoid index
1394 // 0, because index 0 means the longjmp is not ours to handle.
1395 IRB.SetInsertPoint(CI);
1396 Value *Args[] = {CI->getArgOperand(i: 0), IRB.getInt32(C: SetjmpRetPHIs.size()),
1397 FunctionInvocationId};
1398 IRB.CreateCall(Callee: WasmSetjmpF, Args);
1399 ToErase.push_back(Elt: CI);
1400 }
1401
1402 // Handle longjmpable calls.
1403 if (EnableEmSjLj)
1404 handleLongjmpableCallsForEmscriptenSjLj(F, FunctionInvocationId,
1405 SetjmpRetPHIs);
1406 else // EnableWasmSjLj
1407 handleLongjmpableCallsForWasmSjLj(F, FunctionInvocationId, SetjmpRetPHIs);
1408
1409 // Erase everything we no longer need in this function
1410 for (Instruction *I : ToErase)
1411 I->eraseFromParent();
1412
1413 // Finally, our modifications to the cfg can break dominance of SSA variables.
1414 // For example, in this code,
1415 // if (x()) { .. setjmp() .. }
1416 // if (y()) { .. longjmp() .. }
1417 // We must split the longjmp block, and it can jump into the block split from
1418 // setjmp one. But that means that when we split the setjmp block, it's first
1419 // part no longer dominates its second part - there is a theoretically
1420 // possible control flow path where x() is false, then y() is true and we
1421 // reach the second part of the setjmp block, without ever reaching the first
1422 // part. So, we rebuild SSA form here.
1423 rebuildSSA(F);
1424 return true;
1425}
1426
1427// Update each call that can longjmp so it can return to the corresponding
1428// setjmp. Refer to 4) of "Emscripten setjmp/longjmp handling" section in the
1429// comments at top of the file for details.
1430void WebAssemblyLowerEmscriptenEHSjLjImpl::
1431 handleLongjmpableCallsForEmscriptenSjLj(
1432 Function &F, Instruction *FunctionInvocationId,
1433 SmallVectorImpl<PHINode *> &SetjmpRetPHIs) {
1434 Module &M = *F.getParent();
1435 LLVMContext &C = F.getContext();
1436 IRBuilder<> IRB(C);
1437 SmallVector<Instruction *, 64> ToErase;
1438
1439 // call.em.longjmp BB that will be shared within the function.
1440 BasicBlock *CallEmLongjmpBB = nullptr;
1441 // PHI node for the loaded value of __THREW__ global variable in
1442 // call.em.longjmp BB
1443 PHINode *CallEmLongjmpBBThrewPHI = nullptr;
1444 // PHI node for the loaded value of __threwValue global variable in
1445 // call.em.longjmp BB
1446 PHINode *CallEmLongjmpBBThrewValuePHI = nullptr;
1447 // rethrow.exn BB that will be shared within the function.
1448 BasicBlock *RethrowExnBB = nullptr;
1449
1450 // Because we are creating new BBs while processing and don't want to make
1451 // all these newly created BBs candidates again for longjmp processing, we
1452 // first make the vector of candidate BBs.
1453 std::vector<BasicBlock *> BBs;
1454 for (BasicBlock &BB : F)
1455 BBs.push_back(x: &BB);
1456
1457 // BBs.size() will change within the loop, so we query it every time
1458 for (unsigned I = 0; I < BBs.size(); I++) {
1459 BasicBlock *BB = BBs[I];
1460 for (Instruction &I : *BB) {
1461 if (isa<InvokeInst>(Val: &I)) {
1462 std::string S;
1463 raw_string_ostream SS(S);
1464 SS << "In function " << F.getName()
1465 << ": When using Wasm EH with Emscripten SjLj, there is a "
1466 "restriction that `setjmp` function call and exception cannot be "
1467 "used within the same function:\n";
1468 SS << I;
1469 report_fatal_error(reason: StringRef(SS.str()));
1470 }
1471 auto *CI = dyn_cast<CallInst>(Val: &I);
1472 if (!CI)
1473 continue;
1474
1475 const Value *Callee = CI->getCalledOperand();
1476 if (!canLongjmp(Callee))
1477 continue;
1478 if (isEmAsmCall(Callee))
1479 report_fatal_error(reason: "Cannot use EM_ASM* alongside setjmp/longjmp in " +
1480 F.getName() +
1481 ". Please consider using EM_JS, or move the "
1482 "EM_ASM into another function.",
1483 gen_crash_diag: false);
1484
1485 Value *Threw = nullptr;
1486 BasicBlock *Tail;
1487 if (Callee->getName().starts_with(Prefix: "__invoke_")) {
1488 // If invoke wrapper has already been generated for this call in
1489 // previous EH phase, search for the load instruction
1490 // %__THREW__.val = __THREW__;
1491 // in postamble after the invoke wrapper call
1492 LoadInst *ThrewLI = nullptr;
1493 StoreInst *ThrewResetSI = nullptr;
1494 for (auto I = std::next(x: BasicBlock::iterator(CI)), IE = BB->end();
1495 I != IE; ++I) {
1496 if (auto *LI = dyn_cast<LoadInst>(Val&: I))
1497 if (auto *GV = dyn_cast<GlobalVariable>(Val: LI->getPointerOperand()))
1498 if (GV == ThrewGV) {
1499 Threw = ThrewLI = LI;
1500 break;
1501 }
1502 }
1503 // Search for the store instruction after the load above
1504 // __THREW__ = 0;
1505 for (auto I = std::next(x: BasicBlock::iterator(ThrewLI)), IE = BB->end();
1506 I != IE; ++I) {
1507 if (auto *SI = dyn_cast<StoreInst>(Val&: I)) {
1508 if (auto *GV = dyn_cast<GlobalVariable>(Val: SI->getPointerOperand())) {
1509 if (GV == ThrewGV &&
1510 SI->getValueOperand() == getAddrSizeInt(M: &M, C: 0)) {
1511 ThrewResetSI = SI;
1512 break;
1513 }
1514 }
1515 }
1516 }
1517 assert(Threw && ThrewLI && "Cannot find __THREW__ load after invoke");
1518 assert(ThrewResetSI && "Cannot find __THREW__ store after invoke");
1519 Tail = SplitBlock(Old: BB, SplitPt: ThrewResetSI->getNextNode());
1520
1521 } else {
1522 // Wrap call with invoke wrapper and generate preamble/postamble
1523 Threw = wrapInvoke(CI);
1524 ToErase.push_back(Elt: CI);
1525 Tail = SplitBlock(Old: BB, SplitPt: CI->getNextNode());
1526
1527 // If exception handling is enabled, the thrown value can be not a
1528 // longjmp but an exception, in which case we shouldn't silently ignore
1529 // exceptions; we should rethrow them.
1530 // __THREW__'s value is 0 when nothing happened, 1 when an exception is
1531 // thrown, other values when longjmp is thrown.
1532 //
1533 // if (%__THREW__.val == 1)
1534 // goto %eh.rethrow
1535 // else
1536 // goto %normal
1537 //
1538 // eh.rethrow: ;; Rethrow exception
1539 // %exn = call @__cxa_find_matching_catch_2() ;; Retrieve thrown ptr
1540 // __resumeException(%exn)
1541 //
1542 // normal:
1543 // <-- Insertion point. Will insert sjlj handling code from here
1544 // goto %tail
1545 //
1546 // tail:
1547 // ...
1548 if (supportsException(F: &F) && canThrow(V: Callee)) {
1549 // We will add a new conditional branch. So remove the branch created
1550 // when we split the BB
1551 ToErase.push_back(Elt: BB->getTerminator());
1552
1553 // Generate rethrow.exn BB once and share it within the function
1554 if (!RethrowExnBB) {
1555 RethrowExnBB = BasicBlock::Create(Context&: C, Name: "rethrow.exn", Parent: &F);
1556 IRB.SetInsertPoint(RethrowExnBB);
1557 CallInst *Exn =
1558 IRB.CreateCall(Callee: getFindMatchingCatch(M, NumClauses: 0), Args: {}, Name: "exn");
1559 IRB.CreateCall(Callee: ResumeF, Args: {Exn});
1560 IRB.CreateUnreachable();
1561 }
1562
1563 IRB.SetInsertPoint(CI);
1564 BasicBlock *NormalBB = BasicBlock::Create(Context&: C, Name: "normal", Parent: &F);
1565 Value *CmpEqOne =
1566 IRB.CreateICmpEQ(LHS: Threw, RHS: getAddrSizeInt(M: &M, C: 1), Name: "cmp.eq.one");
1567 IRB.CreateCondBr(Cond: CmpEqOne, True: RethrowExnBB, False: NormalBB);
1568
1569 IRB.SetInsertPoint(NormalBB);
1570 IRB.CreateBr(Dest: Tail);
1571 BB = NormalBB; // New insertion point to insert __wasm_setjmp_test()
1572 }
1573 }
1574
1575 // We need to replace the terminator in Tail - SplitBlock makes BB go
1576 // straight to Tail, we need to check if a longjmp occurred, and go to the
1577 // right setjmp-tail if so
1578 ToErase.push_back(Elt: BB->getTerminator());
1579
1580 // Generate a function call to __wasm_setjmp_test function and
1581 // preamble/postamble code to figure out (1) whether longjmp
1582 // occurred (2) if longjmp occurred, which setjmp it corresponds to
1583 Value *Label = nullptr;
1584 Value *LongjmpResult = nullptr;
1585 BasicBlock *EndBB = nullptr;
1586 wrapTestSetjmp(BB, DL: CI->getDebugLoc(), Threw, FunctionInvocationId, Label,
1587 LongjmpResult, CallEmLongjmpBB, CallEmLongjmpBBThrewPHI,
1588 CallEmLongjmpBBThrewValuePHI, EndBB);
1589 assert(Label && LongjmpResult && EndBB);
1590
1591 // Create switch instruction
1592 IRB.SetInsertPoint(EndBB);
1593 IRB.SetCurrentDebugLocation(EndBB->back().getDebugLoc());
1594 SwitchInst *SI = IRB.CreateSwitch(V: Label, Dest: Tail, NumCases: SetjmpRetPHIs.size());
1595 // -1 means no longjmp happened, continue normally (will hit the default
1596 // switch case). 0 means a longjmp that is not ours to handle, needs a
1597 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1598 // 0).
1599 for (unsigned I = 0; I < SetjmpRetPHIs.size(); I++) {
1600 SI->addCase(OnVal: IRB.getInt32(C: I + 1), Dest: SetjmpRetPHIs[I]->getParent());
1601 SetjmpRetPHIs[I]->addIncoming(V: LongjmpResult, BB: EndBB);
1602 }
1603
1604 // We are splitting the block here, and must continue to find other calls
1605 // in the block - which is now split. so continue to traverse in the Tail
1606 BBs.push_back(x: Tail);
1607 }
1608 }
1609
1610 for (Instruction *I : ToErase)
1611 I->eraseFromParent();
1612}
1613
1614static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CPI) {
1615 for (const User *U : CPI->users())
1616 if (const auto *CRI = dyn_cast<CleanupReturnInst>(Val: U))
1617 return CRI->getUnwindDest();
1618 return nullptr;
1619}
1620
1621// Create a catchpad in which we catch a longjmp's env and val arguments, test
1622// if the longjmp corresponds to one of setjmps in the current function, and if
1623// so, jump to the setjmp dispatch BB from which we go to one of post-setjmp
1624// BBs. Refer to 4) of "Wasm setjmp/longjmp handling" section in the comments at
1625// top of the file for details.
1626void WebAssemblyLowerEmscriptenEHSjLjImpl::handleLongjmpableCallsForWasmSjLj(
1627 Function &F, Instruction *FunctionInvocationId,
1628 SmallVectorImpl<PHINode *> &SetjmpRetPHIs) {
1629 Module &M = *F.getParent();
1630 LLVMContext &C = F.getContext();
1631 IRBuilder<> IRB(C);
1632
1633 // A function with catchswitch/catchpad instruction should have a personality
1634 // function attached to it. Search for the wasm personality function, and if
1635 // it exists, use it, and if it doesn't, create a dummy personality function.
1636 // (SjLj is not going to call it anyway.)
1637 if (!F.hasPersonalityFn()) {
1638 StringRef PersName = getEHPersonalityName(Pers: EHPersonality::Wasm_CXX);
1639 FunctionType *PersType =
1640 FunctionType::get(Result: IRB.getInt32Ty(), /* isVarArg */ true);
1641 Value *PersF = M.getOrInsertFunction(Name: PersName, T: PersType).getCallee();
1642 F.setPersonalityFn(
1643 cast<Constant>(Val: IRB.CreateBitCast(V: PersF, DestTy: IRB.getPtrTy())));
1644 }
1645
1646 // Use the entry BB's debugloc as a fallback
1647 BasicBlock *Entry = &F.getEntryBlock();
1648 DebugLoc FirstDL = getOrCreateDebugLoc(InsertBefore: &*Entry->begin(), SP: F.getSubprogram());
1649 IRB.SetCurrentDebugLocation(FirstDL);
1650
1651 // Add setjmp.dispatch BB right after the entry block. Because we have
1652 // initialized functionInvocationId in the entry block and split the
1653 // rest into another BB, here 'OrigEntry' is the function's original entry
1654 // block before the transformation.
1655 //
1656 // entry:
1657 // functionInvocationId initialization
1658 // setjmp.dispatch:
1659 // switch will be inserted here later
1660 // entry.split: (OrigEntry)
1661 // the original function starts here
1662 BasicBlock *OrigEntry = Entry->getNextNode();
1663 BasicBlock *SetjmpDispatchBB =
1664 BasicBlock::Create(Context&: C, Name: "setjmp.dispatch", Parent: &F, InsertBefore: OrigEntry);
1665 cast<UncondBrInst>(Val: Entry->getTerminator())->setSuccessor(SetjmpDispatchBB);
1666
1667 // Create catch.dispatch.longjmp BB and a catchswitch instruction
1668 BasicBlock *CatchDispatchLongjmpBB =
1669 BasicBlock::Create(Context&: C, Name: "catch.dispatch.longjmp", Parent: &F);
1670 IRB.SetInsertPoint(CatchDispatchLongjmpBB);
1671 CatchSwitchInst *CatchSwitchLongjmp =
1672 IRB.CreateCatchSwitch(ParentPad: ConstantTokenNone::get(Context&: C), UnwindBB: nullptr, NumHandlers: 1);
1673
1674 // Create catch.longjmp BB and a catchpad instruction
1675 BasicBlock *CatchLongjmpBB = BasicBlock::Create(Context&: C, Name: "catch.longjmp", Parent: &F);
1676 CatchSwitchLongjmp->addHandler(Dest: CatchLongjmpBB);
1677 IRB.SetInsertPoint(CatchLongjmpBB);
1678 CatchPadInst *CatchPad = IRB.CreateCatchPad(ParentPad: CatchSwitchLongjmp, Args: {});
1679
1680 // Wasm throw and catch instructions can throw and catch multiple values, but
1681 // that requires multivalue support in the toolchain, which is currently not
1682 // very reliable. We instead throw and catch a pointer to a struct value of
1683 // type 'struct __WasmLongjmpArgs', which is defined in Emscripten.
1684 Instruction *LongjmpArgs =
1685 IRB.CreateCall(Callee: CatchF, Args: {IRB.getInt32(C: WebAssembly::C_LONGJMP)}, Name: "thrown");
1686 Value *EnvField =
1687 IRB.CreateConstGEP2_32(Ty: LongjmpArgsTy, Ptr: LongjmpArgs, Idx0: 0, Idx1: 0, Name: "env_gep");
1688 Value *ValField =
1689 IRB.CreateConstGEP2_32(Ty: LongjmpArgsTy, Ptr: LongjmpArgs, Idx0: 0, Idx1: 1, Name: "val_gep");
1690 // void *env = __wasm_longjmp_args.env;
1691 Instruction *Env = IRB.CreateLoad(Ty: IRB.getPtrTy(), Ptr: EnvField, Name: "env");
1692 // int val = __wasm_longjmp_args.val;
1693 Instruction *Val = IRB.CreateLoad(Ty: IRB.getInt32Ty(), Ptr: ValField, Name: "val");
1694
1695 // %label = __wasm_setjmp_test(%env, functionInvocatinoId);
1696 // if (%label == 0)
1697 // __wasm_longjmp(%env, %val)
1698 // catchret to %setjmp.dispatch
1699 BasicBlock *ThenBB = BasicBlock::Create(Context&: C, Name: "if.then", Parent: &F);
1700 BasicBlock *EndBB = BasicBlock::Create(Context&: C, Name: "if.end", Parent: &F);
1701 Value *EnvP = IRB.CreateBitCast(V: Env, DestTy: getAddrPtrType(M: &M), Name: "env.p");
1702 Value *Label = IRB.CreateCall(Callee: WasmSetjmpTestF, Args: {EnvP, FunctionInvocationId},
1703 OpBundles: OperandBundleDef("funclet", CatchPad), Name: "label");
1704 Value *Cmp = IRB.CreateICmpEQ(LHS: Label, RHS: IRB.getInt32(C: 0));
1705 IRB.CreateCondBr(Cond: Cmp, True: ThenBB, False: EndBB);
1706
1707 IRB.SetInsertPoint(ThenBB);
1708 CallInst *WasmLongjmpCI = IRB.CreateCall(
1709 Callee: WasmLongjmpF, Args: {Env, Val}, OpBundles: OperandBundleDef("funclet", CatchPad));
1710 IRB.CreateUnreachable();
1711
1712 IRB.SetInsertPoint(EndBB);
1713 // Jump to setjmp.dispatch block
1714 IRB.CreateCatchRet(CatchPad, BB: SetjmpDispatchBB);
1715
1716 // Go back to setjmp.dispatch BB
1717 // setjmp.dispatch:
1718 // switch %label {
1719 // label 1: goto post-setjmp BB 1
1720 // label 2: goto post-setjmp BB 2
1721 // ...
1722 // default: goto split next BB
1723 // }
1724 IRB.SetInsertPoint(SetjmpDispatchBB);
1725 PHINode *LabelPHI = IRB.CreatePHI(Ty: IRB.getInt32Ty(), NumReservedValues: 2, Name: "label.phi");
1726 LabelPHI->addIncoming(V: Label, BB: EndBB);
1727 LabelPHI->addIncoming(V: IRB.getInt32(C: -1), BB: Entry);
1728 SwitchInst *SI = IRB.CreateSwitch(V: LabelPHI, Dest: OrigEntry, NumCases: SetjmpRetPHIs.size());
1729 // -1 means no longjmp happened, continue normally (will hit the default
1730 // switch case). 0 means a longjmp that is not ours to handle, needs a
1731 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1732 // 0).
1733 for (unsigned I = 0; I < SetjmpRetPHIs.size(); I++) {
1734 SI->addCase(OnVal: IRB.getInt32(C: I + 1), Dest: SetjmpRetPHIs[I]->getParent());
1735 SetjmpRetPHIs[I]->addIncoming(V: Val, BB: SetjmpDispatchBB);
1736 }
1737
1738 // Convert all longjmpable call instructions to invokes that unwind to the
1739 // newly created catch.dispatch.longjmp BB.
1740 SmallVector<CallInst *, 64> LongjmpableCalls;
1741 for (auto *BB = &*F.begin(); BB; BB = BB->getNextNode()) {
1742 for (auto &I : *BB) {
1743 auto *CI = dyn_cast<CallInst>(Val: &I);
1744 if (!CI)
1745 continue;
1746 const Value *Callee = CI->getCalledOperand();
1747 if (!canLongjmp(Callee))
1748 continue;
1749 if (isEmAsmCall(Callee))
1750 report_fatal_error(reason: "Cannot use EM_ASM* alongside setjmp/longjmp in " +
1751 F.getName() +
1752 ". Please consider using EM_JS, or move the "
1753 "EM_ASM into another function.",
1754 gen_crash_diag: false);
1755 // This is __wasm_longjmp() call we inserted in this function, which
1756 // rethrows the longjmp when the longjmp does not correspond to one of
1757 // setjmps in this function. We should not convert this call to an invoke.
1758 if (CI == WasmLongjmpCI)
1759 continue;
1760 LongjmpableCalls.push_back(Elt: CI);
1761 }
1762 }
1763
1764 SmallMapVector<BasicBlock *, SmallSetVector<BasicBlock *, 4>, 4>
1765 UnwindDestToNewPreds;
1766 for (auto *CI : LongjmpableCalls) {
1767 // Even if the callee function has attribute 'nounwind', which is true for
1768 // all C functions, it can longjmp, which means it can throw a Wasm
1769 // exception now.
1770 CI->removeFnAttr(Kind: Attribute::NoUnwind);
1771 if (Function *CalleeF = CI->getCalledFunction())
1772 CalleeF->removeFnAttr(Kind: Attribute::NoUnwind);
1773
1774 // Change it to an invoke and make it unwind to the catch.dispatch.longjmp
1775 // BB. If the call is enclosed in another catchpad/cleanuppad scope, unwind
1776 // to its parent pad's unwind destination instead to preserve the scope
1777 // structure. It will eventually unwind to the catch.dispatch.longjmp.
1778 BasicBlock *UnwindDest = nullptr;
1779 if (auto Bundle = CI->getOperandBundle(ID: LLVMContext::OB_funclet)) {
1780 Instruction *FromPad = cast<Instruction>(Val: Bundle->Inputs[0]);
1781 while (!UnwindDest) {
1782 if (auto *CPI = dyn_cast<CatchPadInst>(Val: FromPad)) {
1783 UnwindDest = CPI->getCatchSwitch()->getUnwindDest();
1784 break;
1785 }
1786 if (auto *CPI = dyn_cast<CleanupPadInst>(Val: FromPad)) {
1787 // getCleanupRetUnwindDest() can return nullptr when
1788 // 1. This cleanuppad's matching cleanupret uwninds to caller
1789 // 2. There is no matching cleanupret because it ends with
1790 // unreachable.
1791 // In case of 2, we need to traverse the parent pad chain.
1792 UnwindDest = getCleanupRetUnwindDest(CPI);
1793 Value *ParentPad = CPI->getParentPad();
1794 if (isa<ConstantTokenNone>(Val: ParentPad))
1795 break;
1796 FromPad = cast<Instruction>(Val: ParentPad);
1797 }
1798 }
1799 }
1800 if (!UnwindDest)
1801 UnwindDest = CatchDispatchLongjmpBB;
1802 // Because we are changing a longjmpable call to an invoke, its unwind
1803 // destination can be an existing EH pad that already have phis, and the BB
1804 // with the newly created invoke will become a new predecessor of that EH
1805 // pad. In this case we need to add the new predecessor to those phis.
1806 UnwindDestToNewPreds[UnwindDest].insert(X: CI->getParent());
1807 changeToInvokeAndSplitBasicBlock(CI, UnwindEdge: UnwindDest);
1808 }
1809
1810 SmallVector<Instruction *, 16> ToErase;
1811 for (auto &BB : F) {
1812 if (auto *CSI = dyn_cast<CatchSwitchInst>(Val: BB.getFirstNonPHIIt())) {
1813 if (CSI != CatchSwitchLongjmp && CSI->unwindsToCaller()) {
1814 IRB.SetInsertPoint(CSI);
1815 ToErase.push_back(Elt: CSI);
1816 auto *NewCSI = IRB.CreateCatchSwitch(ParentPad: CSI->getParentPad(),
1817 UnwindBB: CatchDispatchLongjmpBB, NumHandlers: 1);
1818 NewCSI->addHandler(Dest: *CSI->handler_begin());
1819 NewCSI->takeName(V: CSI);
1820 CSI->replaceAllUsesWith(V: NewCSI);
1821 }
1822 }
1823
1824 if (auto *CRI = dyn_cast<CleanupReturnInst>(Val: BB.getTerminator())) {
1825 if (CRI->unwindsToCaller()) {
1826 IRB.SetInsertPoint(CRI);
1827 ToErase.push_back(Elt: CRI);
1828 IRB.CreateCleanupRet(CleanupPad: CRI->getCleanupPad(), UnwindBB: CatchDispatchLongjmpBB);
1829 }
1830 }
1831 }
1832
1833 for (Instruction *I : ToErase)
1834 I->eraseFromParent();
1835
1836 // Add entries for new predecessors to phis in unwind destinations. We use
1837 // 'poison' as a placeholder value. We should make sure the phis have a valid
1838 // set of predecessors before running SSAUpdater, because SSAUpdater
1839 // internally can use existing phis to gather predecessor info rather than
1840 // scanning the actual CFG (See FindPredecessorBlocks in SSAUpdater.cpp for
1841 // details).
1842 for (auto &[UnwindDest, NewPreds] : UnwindDestToNewPreds) {
1843 for (PHINode &PN : UnwindDest->phis()) {
1844 for (auto *NewPred : NewPreds) {
1845 assert(PN.getBasicBlockIndex(NewPred) == -1);
1846 PN.addIncoming(V: PoisonValue::get(T: PN.getType()), BB: NewPred);
1847 }
1848 }
1849 }
1850
1851 // For unwind destinations for newly added invokes to longjmpable functions,
1852 // calculate incoming values for the newly added predecessors using
1853 // SSAUpdater. We add existing values in the phis to SSAUpdater as available
1854 // values and let it calculate what the value should be at the end of new
1855 // incoming blocks.
1856 for (auto &[UnwindDest, NewPreds] : UnwindDestToNewPreds) {
1857 for (PHINode &PN : UnwindDest->phis()) {
1858 SSAUpdater SSA;
1859 SSA.Initialize(Ty: PN.getType(), Name: PN.getName());
1860 for (unsigned Idx = 0, E = PN.getNumIncomingValues(); Idx != E; ++Idx) {
1861 if (NewPreds.contains(key: PN.getIncomingBlock(i: Idx)))
1862 continue;
1863 Value *V = PN.getIncomingValue(i: Idx);
1864 if (auto *II = dyn_cast<InvokeInst>(Val: V))
1865 SSA.AddAvailableValue(BB: II->getNormalDest(), V: II);
1866 else if (auto *I = dyn_cast<Instruction>(Val: V))
1867 SSA.AddAvailableValue(BB: I->getParent(), V: I);
1868 else
1869 SSA.AddAvailableValue(BB: PN.getIncomingBlock(i: Idx), V);
1870 }
1871 for (auto *NewPred : NewPreds)
1872 PN.setIncomingValueForBlock(BB: NewPred, V: SSA.GetValueAtEndOfBlock(BB: NewPred));
1873 assert(PN.isComplete());
1874 }
1875 }
1876}
1877
1878bool WebAssemblyLowerEmscriptenEHSjLjLegacy::runOnModule(Module &M) {
1879 WebAssemblyLowerEmscriptenEHSjLjImpl Impl(
1880 EnableEmEH, [&](Function &F) -> DominatorTree & {
1881 return getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
1882 });
1883 return Impl.runOnModule(M);
1884}
1885
1886PreservedAnalyses
1887WebAssemblyLowerEmscriptenEHSjLjPass::run(Module &M,
1888 ModuleAnalysisManager &MAM) {
1889 WebAssemblyLowerEmscriptenEHSjLjImpl Impl(
1890 EnableEmEH, [&](Function &F) -> DominatorTree & {
1891 return MAM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M)
1892 .getManager()
1893 .getResult<DominatorTreeAnalysis>(IR&: F);
1894 });
1895 return Impl.runOnModule(M) ? PreservedAnalyses::none()
1896 : PreservedAnalyses::all();
1897}
1898