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 11 /// function calls in order to use Emscripten's JavaScript try and catch 12 /// mechanism. 13 /// 14 /// To handle exceptions and setjmp/longjmps, this scheme relies on JavaScript's 15 /// try and catch syntax and relevant exception-related libraries implemented 16 /// in JavaScript glue code that will be produced by Emscripten. This is similar 17 /// to the current Emscripten asm.js exception handling in fastcomp. For 18 /// fastcomp's EH / SjLj scheme, see these files in fastcomp LLVM branch: 19 /// (Location: https://github.com/kripken/emscripten-fastcomp) 20 /// lib/Target/JSBackend/NaCl/LowerEmExceptionsPass.cpp 21 /// lib/Target/JSBackend/NaCl/LowerEmSetjmp.cpp 22 /// lib/Target/JSBackend/JSBackend.cpp 23 /// lib/Target/JSBackend/CallHandlers.h 24 /// 25 /// * Exception handling 26 /// This pass lowers invokes and landingpads into library functions in JS glue 27 /// code. Invokes are lowered into function wrappers called invoke wrappers that 28 /// exist in JS side, which wraps the original function call with JS try-catch. 29 /// If an exception occurred, cxa_throw() function in JS side sets some 30 /// variables (see below) so we can check whether an exception occurred from 31 /// wasm code and handle it appropriately. 32 /// 33 /// * Setjmp-longjmp handling 34 /// This pass lowers setjmp to a reasonably-performant approach for emscripten. 35 /// The idea is that each block with a setjmp is broken up into two parts: the 36 /// part containing setjmp and the part right after the setjmp. The latter part 37 /// is either reached from the setjmp, or later from a longjmp. To handle the 38 /// longjmp, all calls that might longjmp are also called using invoke wrappers 39 /// and thus JS / try-catch. JS longjmp() function also sets some variables so 40 /// we can check / whether a longjmp occurred from wasm code. Each block with a 41 /// function call that might longjmp is also split up after the longjmp call. 42 /// After the longjmp call, we check whether a longjmp occurred, and if it did, 43 /// which setjmp it corresponds to, and jump to the right post-setjmp block. 44 /// We assume setjmp-longjmp handling always run after EH handling, which means 45 /// we don't expect any exception-related instructions when SjLj runs. 46 /// FIXME Currently this scheme does not support indirect call of setjmp, 47 /// because of the limitation of the scheme itself. fastcomp does not support it 48 /// either. 49 /// 50 /// In detail, this pass does following things: 51 /// 52 /// 1) Assumes the existence of global variables: __THREW__, __threwValue 53 /// __THREW__ and __threwValue will be set in invoke wrappers 54 /// in JS glue code. For what invoke wrappers are, refer to 3). These 55 /// variables are used for both exceptions and setjmp/longjmps. 56 /// __THREW__ indicates whether an exception or a longjmp occurred or not. 0 57 /// means nothing occurred, 1 means an exception occurred, and other numbers 58 /// mean a longjmp occurred. In the case of longjmp, __threwValue variable 59 /// indicates the corresponding setjmp buffer the longjmp corresponds to. 60 /// 61 /// * Exception handling 62 /// 63 /// 2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions 64 /// at link time. 65 /// The global variables in 1) will exist in wasm address space, 66 /// but their values should be set in JS code, so these functions 67 /// as interfaces to JS glue code. These functions are equivalent to the 68 /// following JS functions, which actually exist in asm.js version of JS 69 /// library. 70 /// 71 /// function setThrew(threw, value) { 72 /// if (__THREW__ == 0) { 73 /// __THREW__ = threw; 74 /// __threwValue = value; 75 /// } 76 /// } 77 // 78 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code. 79 /// 80 /// In exception handling, getTempRet0 indicates the type of an exception 81 /// caught, and in setjmp/longjmp, it means the second argument to longjmp 82 /// function. 83 /// 84 /// 3) Lower 85 /// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad 86 /// into 87 /// __THREW__ = 0; 88 /// call @__invoke_SIG(func, arg1, arg2) 89 /// %__THREW__.val = __THREW__; 90 /// __THREW__ = 0; 91 /// if (%__THREW__.val == 1) 92 /// goto %lpad 93 /// else 94 /// goto %invoke.cont 95 /// SIG is a mangled string generated based on the LLVM IR-level function 96 /// signature. After LLVM IR types are lowered to the target wasm types, 97 /// the names for these wrappers will change based on wasm types as well, 98 /// as in invoke_vi (function takes an int and returns void). The bodies of 99 /// these wrappers will be generated in JS glue code, and inside those 100 /// wrappers we use JS try-catch to generate actual exception effects. It 101 /// also calls the original callee function. An example wrapper in JS code 102 /// would look like this: 103 /// function invoke_vi(index,a1) { 104 /// try { 105 /// Module["dynCall_vi"](index,a1); // This calls original callee 106 /// } catch(e) { 107 /// if (typeof e !== 'number' && e !== 'longjmp') throw e; 108 /// asm["setThrew"](1, 0); // setThrew is called here 109 /// } 110 /// } 111 /// If an exception is thrown, __THREW__ will be set to true in a wrapper, 112 /// so we can jump to the right BB based on this value. 113 /// 114 /// 4) Lower 115 /// %val = landingpad catch c1 catch c2 catch c3 ... 116 /// ... use %val ... 117 /// into 118 /// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...) 119 /// %val = {%fmc, getTempRet0()} 120 /// ... use %val ... 121 /// Here N is a number calculated based on the number of clauses. 122 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code. 123 /// 124 /// 5) Lower 125 /// resume {%a, %b} 126 /// into 127 /// call @__resumeException(%a) 128 /// where __resumeException() is a function in JS glue code. 129 /// 130 /// 6) Lower 131 /// call @llvm.eh.typeid.for(type) (intrinsic) 132 /// into 133 /// call @llvm_eh_typeid_for(type) 134 /// llvm_eh_typeid_for function will be generated in JS glue code. 135 /// 136 /// * Setjmp / Longjmp handling 137 /// 138 /// In case calls to longjmp() exists 139 /// 140 /// 1) Lower 141 /// longjmp(buf, value) 142 /// into 143 /// emscripten_longjmp_jmpbuf(buf, value) 144 /// emscripten_longjmp_jmpbuf will be lowered to emscripten_longjmp later. 145 /// 146 /// In case calls to setjmp() exists 147 /// 148 /// 2) In the function entry that calls setjmp, initialize setjmpTable and 149 /// sejmpTableSize as follows: 150 /// setjmpTableSize = 4; 151 /// setjmpTable = (int *) malloc(40); 152 /// setjmpTable[0] = 0; 153 /// setjmpTable and setjmpTableSize are used in saveSetjmp() function in JS 154 /// code. 155 /// 156 /// 3) Lower 157 /// setjmp(buf) 158 /// into 159 /// setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize); 160 /// setjmpTableSize = getTempRet0(); 161 /// For each dynamic setjmp call, setjmpTable stores its ID (a number which 162 /// is incrementally assigned from 0) and its label (a unique number that 163 /// represents each callsite of setjmp). When we need more entries in 164 /// setjmpTable, it is reallocated in saveSetjmp() in JS code and it will 165 /// return the new table address, and assign the new table size in 166 /// setTempRet0(). saveSetjmp also stores the setjmp's ID into the buffer 167 /// buf. A BB with setjmp is split into two after setjmp call in order to 168 /// make the post-setjmp BB the possible destination of longjmp BB. 169 /// 170 /// 171 /// 4) Lower every call that might longjmp into 172 /// __THREW__ = 0; 173 /// call @__invoke_SIG(func, arg1, arg2) 174 /// %__THREW__.val = __THREW__; 175 /// __THREW__ = 0; 176 /// if (%__THREW__.val != 0 & __threwValue != 0) { 177 /// %label = testSetjmp(mem[%__THREW__.val], setjmpTable, 178 /// setjmpTableSize); 179 /// if (%label == 0) 180 /// emscripten_longjmp(%__THREW__.val, __threwValue); 181 /// setTempRet0(__threwValue); 182 /// } else { 183 /// %label = -1; 184 /// } 185 /// longjmp_result = getTempRet0(); 186 /// switch label { 187 /// label 1: goto post-setjmp BB 1 188 /// label 2: goto post-setjmp BB 2 189 /// ... 190 /// default: goto splitted next BB 191 /// } 192 /// testSetjmp examines setjmpTable to see if there is a matching setjmp 193 /// call. After calling an invoke wrapper, if a longjmp occurred, __THREW__ 194 /// will be the address of matching jmp_buf buffer and __threwValue be the 195 /// second argument to longjmp. mem[__THREW__.val] is a setjmp ID that is 196 /// stored in saveSetjmp. testSetjmp returns a setjmp label, a unique ID to 197 /// each setjmp callsite. Label 0 means this longjmp buffer does not 198 /// correspond to one of the setjmp callsites in this function, so in this 199 /// case we just chain the longjmp to the caller. (Here we call 200 /// emscripten_longjmp, which is different from emscripten_longjmp_jmpbuf. 201 /// emscripten_longjmp_jmpbuf takes jmp_buf as its first argument, while 202 /// emscripten_longjmp takes an int. Both of them will eventually be lowered 203 /// to emscripten_longjmp in s2wasm, but here we need two signatures - we 204 /// can't translate an int value to a jmp_buf.) 205 /// Label -1 means no longjmp occurred. Otherwise we jump to the right 206 /// post-setjmp BB based on the label. 207 /// 208 ///===----------------------------------------------------------------------===// 209 210 #include "WebAssembly.h" 211 #include "WebAssemblyTargetMachine.h" 212 #include "llvm/ADT/StringExtras.h" 213 #include "llvm/CodeGen/TargetPassConfig.h" 214 #include "llvm/IR/DebugInfoMetadata.h" 215 #include "llvm/IR/Dominators.h" 216 #include "llvm/IR/IRBuilder.h" 217 #include "llvm/Support/CommandLine.h" 218 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 219 #include "llvm/Transforms/Utils/SSAUpdater.h" 220 221 using namespace llvm; 222 223 #define DEBUG_TYPE "wasm-lower-em-ehsjlj" 224 225 static cl::list<std::string> 226 EHAllowlist("emscripten-cxx-exceptions-allowed", 227 cl::desc("The list of function names in which Emscripten-style " 228 "exception handling is enabled (see emscripten " 229 "EMSCRIPTEN_CATCHING_ALLOWED options)"), 230 cl::CommaSeparated); 231 232 namespace { 233 class WebAssemblyLowerEmscriptenEHSjLj final : public ModulePass { 234 bool EnableEH; // Enable exception handling 235 bool EnableSjLj; // Enable setjmp/longjmp handling 236 237 GlobalVariable *ThrewGV = nullptr; 238 GlobalVariable *ThrewValueGV = nullptr; 239 Function *GetTempRet0Func = nullptr; 240 Function *SetTempRet0Func = nullptr; 241 Function *ResumeF = nullptr; 242 Function *EHTypeIDF = nullptr; 243 Function *EmLongjmpF = nullptr; 244 Function *EmLongjmpJmpbufF = nullptr; 245 Function *SaveSetjmpF = nullptr; 246 Function *TestSetjmpF = nullptr; 247 248 // __cxa_find_matching_catch_N functions. 249 // Indexed by the number of clauses in an original landingpad instruction. 250 DenseMap<int, Function *> FindMatchingCatches; 251 // Map of <function signature string, invoke_ wrappers> 252 StringMap<Function *> InvokeWrappers; 253 // Set of allowed function names for exception handling 254 std::set<std::string> EHAllowlistSet; 255 256 StringRef getPassName() const override { 257 return "WebAssembly Lower Emscripten Exceptions"; 258 } 259 260 bool runEHOnFunction(Function &F); 261 bool runSjLjOnFunction(Function &F); 262 Function *getFindMatchingCatch(Module &M, unsigned NumClauses); 263 264 Value *wrapInvoke(CallBase *CI); 265 void wrapTestSetjmp(BasicBlock *BB, DebugLoc DL, Value *Threw, 266 Value *SetjmpTable, Value *SetjmpTableSize, Value *&Label, 267 Value *&LongjmpResult, BasicBlock *&EndBB); 268 Function *getInvokeWrapper(CallBase *CI); 269 270 bool areAllExceptionsAllowed() const { return EHAllowlistSet.empty(); } 271 bool canLongjmp(Module &M, const Value *Callee) const; 272 bool isEmAsmCall(Module &M, const Value *Callee) const; 273 274 void rebuildSSA(Function &F); 275 276 public: 277 static char ID; 278 279 WebAssemblyLowerEmscriptenEHSjLj(bool EnableEH = true, bool EnableSjLj = true) 280 : ModulePass(ID), EnableEH(EnableEH), EnableSjLj(EnableSjLj) { 281 EHAllowlistSet.insert(EHAllowlist.begin(), EHAllowlist.end()); 282 } 283 bool runOnModule(Module &M) override; 284 285 void getAnalysisUsage(AnalysisUsage &AU) const override { 286 AU.addRequired<DominatorTreeWrapperPass>(); 287 } 288 }; 289 } // End anonymous namespace 290 291 char WebAssemblyLowerEmscriptenEHSjLj::ID = 0; 292 INITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLj, DEBUG_TYPE, 293 "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp", 294 false, false) 295 296 ModulePass *llvm::createWebAssemblyLowerEmscriptenEHSjLj(bool EnableEH, 297 bool EnableSjLj) { 298 return new WebAssemblyLowerEmscriptenEHSjLj(EnableEH, EnableSjLj); 299 } 300 301 static bool canThrow(const Value *V) { 302 if (const auto *F = dyn_cast<const Function>(V)) { 303 // Intrinsics cannot throw 304 if (F->isIntrinsic()) 305 return false; 306 StringRef Name = F->getName(); 307 // leave setjmp and longjmp (mostly) alone, we process them properly later 308 if (Name == "setjmp" || Name == "longjmp") 309 return false; 310 return !F->doesNotThrow(); 311 } 312 // not a function, so an indirect call - can throw, we can't tell 313 return true; 314 } 315 316 // Get a global variable with the given name. If it doesn't exist declare it, 317 // which will generate an import and asssumes that it will exist at link time. 318 static GlobalVariable *getGlobalVariableI32(Module &M, IRBuilder<> &IRB, 319 WebAssemblyTargetMachine &TM, 320 const char *Name) { 321 auto Int32Ty = IRB.getInt32Ty(); 322 auto *GV = dyn_cast<GlobalVariable>(M.getOrInsertGlobal(Name, Int32Ty)); 323 if (!GV) 324 report_fatal_error(Twine("unable to create global: ") + Name); 325 326 // If the target supports TLS, make this variable thread-local. We can't just 327 // unconditionally make it thread-local and depend on 328 // CoalesceFeaturesAndStripAtomics to downgrade it, because stripping TLS has 329 // the side effect of disallowing the object from being linked into a 330 // shared-memory module, which we don't want to be responsible for. 331 auto *Subtarget = TM.getSubtargetImpl(); 332 auto TLS = Subtarget->hasAtomics() && Subtarget->hasBulkMemory() 333 ? GlobalValue::LocalExecTLSModel 334 : GlobalValue::NotThreadLocal; 335 GV->setThreadLocalMode(TLS); 336 return GV; 337 } 338 339 // Simple function name mangler. 340 // This function simply takes LLVM's string representation of parameter types 341 // and concatenate them with '_'. There are non-alphanumeric characters but llc 342 // is ok with it, and we need to postprocess these names after the lowering 343 // phase anyway. 344 static std::string getSignature(FunctionType *FTy) { 345 std::string Sig; 346 raw_string_ostream OS(Sig); 347 OS << *FTy->getReturnType(); 348 for (Type *ParamTy : FTy->params()) 349 OS << "_" << *ParamTy; 350 if (FTy->isVarArg()) 351 OS << "_..."; 352 Sig = OS.str(); 353 Sig.erase(remove_if(Sig, isSpace), Sig.end()); 354 // When s2wasm parses .s file, a comma means the end of an argument. So a 355 // mangled function name can contain any character but a comma. 356 std::replace(Sig.begin(), Sig.end(), ',', '.'); 357 return Sig; 358 } 359 360 static Function *getEmscriptenFunction(FunctionType *Ty, const Twine &Name, 361 Module *M) { 362 Function* F = Function::Create(Ty, GlobalValue::ExternalLinkage, Name, M); 363 // Tell the linker that this function is expected to be imported from the 364 // 'env' module. 365 if (!F->hasFnAttribute("wasm-import-module")) { 366 llvm::AttrBuilder B; 367 B.addAttribute("wasm-import-module", "env"); 368 F->addAttributes(llvm::AttributeList::FunctionIndex, B); 369 } 370 if (!F->hasFnAttribute("wasm-import-name")) { 371 llvm::AttrBuilder B; 372 B.addAttribute("wasm-import-name", F->getName()); 373 F->addAttributes(llvm::AttributeList::FunctionIndex, B); 374 } 375 return F; 376 } 377 378 // Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2. 379 // This is because a landingpad instruction contains two more arguments, a 380 // personality function and a cleanup bit, and __cxa_find_matching_catch_N 381 // functions are named after the number of arguments in the original landingpad 382 // instruction. 383 Function * 384 WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M, 385 unsigned NumClauses) { 386 if (FindMatchingCatches.count(NumClauses)) 387 return FindMatchingCatches[NumClauses]; 388 PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext()); 389 SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy); 390 FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false); 391 Function *F = getEmscriptenFunction( 392 FTy, "__cxa_find_matching_catch_" + Twine(NumClauses + 2), &M); 393 FindMatchingCatches[NumClauses] = F; 394 return F; 395 } 396 397 // Generate invoke wrapper seqence with preamble and postamble 398 // Preamble: 399 // __THREW__ = 0; 400 // Postamble: 401 // %__THREW__.val = __THREW__; __THREW__ = 0; 402 // Returns %__THREW__.val, which indicates whether an exception is thrown (or 403 // whether longjmp occurred), for future use. 404 Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallBase *CI) { 405 LLVMContext &C = CI->getModule()->getContext(); 406 407 // If we are calling a function that is noreturn, we must remove that 408 // attribute. The code we insert here does expect it to return, after we 409 // catch the exception. 410 if (CI->doesNotReturn()) { 411 if (auto *F = CI->getCalledFunction()) 412 F->removeFnAttr(Attribute::NoReturn); 413 CI->removeAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); 414 } 415 416 IRBuilder<> IRB(C); 417 IRB.SetInsertPoint(CI); 418 419 // Pre-invoke 420 // __THREW__ = 0; 421 IRB.CreateStore(IRB.getInt32(0), ThrewGV); 422 423 // Invoke function wrapper in JavaScript 424 SmallVector<Value *, 16> Args; 425 // Put the pointer to the callee as first argument, so it can be called 426 // within the invoke wrapper later 427 Args.push_back(CI->getCalledOperand()); 428 Args.append(CI->arg_begin(), CI->arg_end()); 429 CallInst *NewCall = IRB.CreateCall(getInvokeWrapper(CI), Args); 430 NewCall->takeName(CI); 431 NewCall->setCallingConv(CallingConv::WASM_EmscriptenInvoke); 432 NewCall->setDebugLoc(CI->getDebugLoc()); 433 434 // Because we added the pointer to the callee as first argument, all 435 // argument attribute indices have to be incremented by one. 436 SmallVector<AttributeSet, 8> ArgAttributes; 437 const AttributeList &InvokeAL = CI->getAttributes(); 438 439 // No attributes for the callee pointer. 440 ArgAttributes.push_back(AttributeSet()); 441 // Copy the argument attributes from the original 442 for (unsigned I = 0, E = CI->getNumArgOperands(); I < E; ++I) 443 ArgAttributes.push_back(InvokeAL.getParamAttributes(I)); 444 445 AttrBuilder FnAttrs(InvokeAL.getFnAttributes()); 446 if (FnAttrs.contains(Attribute::AllocSize)) { 447 // The allocsize attribute (if any) referes to parameters by index and needs 448 // to be adjusted. 449 unsigned SizeArg; 450 Optional<unsigned> NEltArg; 451 std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs(); 452 SizeArg += 1; 453 if (NEltArg.hasValue()) 454 NEltArg = NEltArg.getValue() + 1; 455 FnAttrs.addAllocSizeAttr(SizeArg, NEltArg); 456 } 457 458 // Reconstruct the AttributesList based on the vector we constructed. 459 AttributeList NewCallAL = 460 AttributeList::get(C, AttributeSet::get(C, FnAttrs), 461 InvokeAL.getRetAttributes(), ArgAttributes); 462 NewCall->setAttributes(NewCallAL); 463 464 CI->replaceAllUsesWith(NewCall); 465 466 // Post-invoke 467 // %__THREW__.val = __THREW__; __THREW__ = 0; 468 Value *Threw = 469 IRB.CreateLoad(IRB.getInt32Ty(), ThrewGV, ThrewGV->getName() + ".val"); 470 IRB.CreateStore(IRB.getInt32(0), ThrewGV); 471 return Threw; 472 } 473 474 // Get matching invoke wrapper based on callee signature 475 Function *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallBase *CI) { 476 Module *M = CI->getModule(); 477 SmallVector<Type *, 16> ArgTys; 478 FunctionType *CalleeFTy = CI->getFunctionType(); 479 480 std::string Sig = getSignature(CalleeFTy); 481 if (InvokeWrappers.find(Sig) != InvokeWrappers.end()) 482 return InvokeWrappers[Sig]; 483 484 // Put the pointer to the callee as first argument 485 ArgTys.push_back(PointerType::getUnqual(CalleeFTy)); 486 // Add argument types 487 ArgTys.append(CalleeFTy->param_begin(), CalleeFTy->param_end()); 488 489 FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys, 490 CalleeFTy->isVarArg()); 491 Function *F = getEmscriptenFunction(FTy, "__invoke_" + Sig, M); 492 InvokeWrappers[Sig] = F; 493 return F; 494 } 495 496 bool WebAssemblyLowerEmscriptenEHSjLj::canLongjmp(Module &M, 497 const Value *Callee) const { 498 if (auto *CalleeF = dyn_cast<Function>(Callee)) 499 if (CalleeF->isIntrinsic()) 500 return false; 501 502 // Attempting to transform inline assembly will result in something like: 503 // call void @__invoke_void(void ()* asm ...) 504 // which is invalid because inline assembly blocks do not have addresses 505 // and can't be passed by pointer. The result is a crash with illegal IR. 506 if (isa<InlineAsm>(Callee)) 507 return false; 508 StringRef CalleeName = Callee->getName(); 509 510 // The reason we include malloc/free here is to exclude the malloc/free 511 // calls generated in setjmp prep / cleanup routines. 512 if (CalleeName == "setjmp" || CalleeName == "malloc" || CalleeName == "free") 513 return false; 514 515 // There are functions in JS glue code 516 if (CalleeName == "__resumeException" || CalleeName == "llvm_eh_typeid_for" || 517 CalleeName == "saveSetjmp" || CalleeName == "testSetjmp" || 518 CalleeName == "getTempRet0" || CalleeName == "setTempRet0") 519 return false; 520 521 // __cxa_find_matching_catch_N functions cannot longjmp 522 if (Callee->getName().startswith("__cxa_find_matching_catch_")) 523 return false; 524 525 // Exception-catching related functions 526 if (CalleeName == "__cxa_begin_catch" || CalleeName == "__cxa_end_catch" || 527 CalleeName == "__cxa_allocate_exception" || CalleeName == "__cxa_throw" || 528 CalleeName == "__clang_call_terminate") 529 return false; 530 531 // Otherwise we don't know 532 return true; 533 } 534 535 bool WebAssemblyLowerEmscriptenEHSjLj::isEmAsmCall(Module &M, 536 const Value *Callee) const { 537 StringRef CalleeName = Callee->getName(); 538 // This is an exhaustive list from Emscripten's <emscripten/em_asm.h>. 539 return CalleeName == "emscripten_asm_const_int" || 540 CalleeName == "emscripten_asm_const_double" || 541 CalleeName == "emscripten_asm_const_int_sync_on_main_thread" || 542 CalleeName == "emscripten_asm_const_double_sync_on_main_thread" || 543 CalleeName == "emscripten_asm_const_async_on_main_thread"; 544 } 545 546 // Generate testSetjmp function call seqence with preamble and postamble. 547 // The code this generates is equivalent to the following JavaScript code: 548 // if (%__THREW__.val != 0 & threwValue != 0) { 549 // %label = _testSetjmp(mem[%__THREW__.val], setjmpTable, setjmpTableSize); 550 // if (%label == 0) 551 // emscripten_longjmp(%__THREW__.val, threwValue); 552 // setTempRet0(threwValue); 553 // } else { 554 // %label = -1; 555 // } 556 // %longjmp_result = getTempRet0(); 557 // 558 // As output parameters. returns %label, %longjmp_result, and the BB the last 559 // instruction (%longjmp_result = ...) is in. 560 void WebAssemblyLowerEmscriptenEHSjLj::wrapTestSetjmp( 561 BasicBlock *BB, DebugLoc DL, Value *Threw, Value *SetjmpTable, 562 Value *SetjmpTableSize, Value *&Label, Value *&LongjmpResult, 563 BasicBlock *&EndBB) { 564 Function *F = BB->getParent(); 565 LLVMContext &C = BB->getModule()->getContext(); 566 IRBuilder<> IRB(C); 567 IRB.SetCurrentDebugLocation(DL); 568 569 // if (%__THREW__.val != 0 & threwValue != 0) 570 IRB.SetInsertPoint(BB); 571 BasicBlock *ThenBB1 = BasicBlock::Create(C, "if.then1", F); 572 BasicBlock *ElseBB1 = BasicBlock::Create(C, "if.else1", F); 573 BasicBlock *EndBB1 = BasicBlock::Create(C, "if.end", F); 574 Value *ThrewCmp = IRB.CreateICmpNE(Threw, IRB.getInt32(0)); 575 Value *ThrewValue = IRB.CreateLoad(IRB.getInt32Ty(), ThrewValueGV, 576 ThrewValueGV->getName() + ".val"); 577 Value *ThrewValueCmp = IRB.CreateICmpNE(ThrewValue, IRB.getInt32(0)); 578 Value *Cmp1 = IRB.CreateAnd(ThrewCmp, ThrewValueCmp, "cmp1"); 579 IRB.CreateCondBr(Cmp1, ThenBB1, ElseBB1); 580 581 // %label = _testSetjmp(mem[%__THREW__.val], _setjmpTable, _setjmpTableSize); 582 // if (%label == 0) 583 IRB.SetInsertPoint(ThenBB1); 584 BasicBlock *ThenBB2 = BasicBlock::Create(C, "if.then2", F); 585 BasicBlock *EndBB2 = BasicBlock::Create(C, "if.end2", F); 586 Value *ThrewInt = IRB.CreateIntToPtr(Threw, Type::getInt32PtrTy(C), 587 Threw->getName() + ".i32p"); 588 Value *LoadedThrew = IRB.CreateLoad(IRB.getInt32Ty(), ThrewInt, 589 ThrewInt->getName() + ".loaded"); 590 Value *ThenLabel = IRB.CreateCall( 591 TestSetjmpF, {LoadedThrew, SetjmpTable, SetjmpTableSize}, "label"); 592 Value *Cmp2 = IRB.CreateICmpEQ(ThenLabel, IRB.getInt32(0)); 593 IRB.CreateCondBr(Cmp2, ThenBB2, EndBB2); 594 595 // emscripten_longjmp(%__THREW__.val, threwValue); 596 IRB.SetInsertPoint(ThenBB2); 597 IRB.CreateCall(EmLongjmpF, {Threw, ThrewValue}); 598 IRB.CreateUnreachable(); 599 600 // setTempRet0(threwValue); 601 IRB.SetInsertPoint(EndBB2); 602 IRB.CreateCall(SetTempRet0Func, ThrewValue); 603 IRB.CreateBr(EndBB1); 604 605 IRB.SetInsertPoint(ElseBB1); 606 IRB.CreateBr(EndBB1); 607 608 // longjmp_result = getTempRet0(); 609 IRB.SetInsertPoint(EndBB1); 610 PHINode *LabelPHI = IRB.CreatePHI(IRB.getInt32Ty(), 2, "label"); 611 LabelPHI->addIncoming(ThenLabel, EndBB2); 612 613 LabelPHI->addIncoming(IRB.getInt32(-1), ElseBB1); 614 615 // Output parameter assignment 616 Label = LabelPHI; 617 EndBB = EndBB1; 618 LongjmpResult = IRB.CreateCall(GetTempRet0Func, None, "longjmp_result"); 619 } 620 621 void WebAssemblyLowerEmscriptenEHSjLj::rebuildSSA(Function &F) { 622 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); 623 DT.recalculate(F); // CFG has been changed 624 SSAUpdater SSA; 625 for (BasicBlock &BB : F) { 626 for (Instruction &I : BB) { 627 SSA.Initialize(I.getType(), I.getName()); 628 SSA.AddAvailableValue(&BB, &I); 629 for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) { 630 Use &U = *UI; 631 ++UI; 632 auto *User = cast<Instruction>(U.getUser()); 633 if (auto *UserPN = dyn_cast<PHINode>(User)) 634 if (UserPN->getIncomingBlock(U) == &BB) 635 continue; 636 637 if (DT.dominates(&I, User)) 638 continue; 639 SSA.RewriteUseAfterInsertions(U); 640 } 641 } 642 } 643 } 644 645 bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) { 646 LLVM_DEBUG(dbgs() << "********** Lower Emscripten EH & SjLj **********\n"); 647 648 LLVMContext &C = M.getContext(); 649 IRBuilder<> IRB(C); 650 651 Function *SetjmpF = M.getFunction("setjmp"); 652 Function *LongjmpF = M.getFunction("longjmp"); 653 bool SetjmpUsed = SetjmpF && !SetjmpF->use_empty(); 654 bool LongjmpUsed = LongjmpF && !LongjmpF->use_empty(); 655 bool DoSjLj = EnableSjLj && (SetjmpUsed || LongjmpUsed); 656 657 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); 658 assert(TPC && "Expected a TargetPassConfig"); 659 auto &TM = TPC->getTM<WebAssemblyTargetMachine>(); 660 661 // Declare (or get) global variables __THREW__, __threwValue, and 662 // getTempRet0/setTempRet0 function which are used in common for both 663 // exception handling and setjmp/longjmp handling 664 ThrewGV = getGlobalVariableI32(M, IRB, TM, "__THREW__"); 665 ThrewValueGV = getGlobalVariableI32(M, IRB, TM, "__threwValue"); 666 GetTempRet0Func = getEmscriptenFunction( 667 FunctionType::get(IRB.getInt32Ty(), false), "getTempRet0", &M); 668 SetTempRet0Func = getEmscriptenFunction( 669 FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false), 670 "setTempRet0", &M); 671 GetTempRet0Func->setDoesNotThrow(); 672 SetTempRet0Func->setDoesNotThrow(); 673 674 bool Changed = false; 675 676 // Exception handling 677 if (EnableEH) { 678 // Register __resumeException function 679 FunctionType *ResumeFTy = 680 FunctionType::get(IRB.getVoidTy(), IRB.getInt8PtrTy(), false); 681 ResumeF = getEmscriptenFunction(ResumeFTy, "__resumeException", &M); 682 683 // Register llvm_eh_typeid_for function 684 FunctionType *EHTypeIDTy = 685 FunctionType::get(IRB.getInt32Ty(), IRB.getInt8PtrTy(), false); 686 EHTypeIDF = getEmscriptenFunction(EHTypeIDTy, "llvm_eh_typeid_for", &M); 687 688 for (Function &F : M) { 689 if (F.isDeclaration()) 690 continue; 691 Changed |= runEHOnFunction(F); 692 } 693 } 694 695 // Setjmp/longjmp handling 696 if (DoSjLj) { 697 Changed = true; // We have setjmp or longjmp somewhere 698 699 if (LongjmpF) { 700 // Replace all uses of longjmp with emscripten_longjmp_jmpbuf, which is 701 // defined in JS code 702 EmLongjmpJmpbufF = getEmscriptenFunction(LongjmpF->getFunctionType(), 703 "emscripten_longjmp_jmpbuf", &M); 704 LongjmpF->replaceAllUsesWith(EmLongjmpJmpbufF); 705 } 706 707 if (SetjmpF) { 708 // Register saveSetjmp function 709 FunctionType *SetjmpFTy = SetjmpF->getFunctionType(); 710 FunctionType *FTy = 711 FunctionType::get(Type::getInt32PtrTy(C), 712 {SetjmpFTy->getParamType(0), IRB.getInt32Ty(), 713 Type::getInt32PtrTy(C), IRB.getInt32Ty()}, 714 false); 715 SaveSetjmpF = getEmscriptenFunction(FTy, "saveSetjmp", &M); 716 717 // Register testSetjmp function 718 FTy = FunctionType::get( 719 IRB.getInt32Ty(), 720 {IRB.getInt32Ty(), Type::getInt32PtrTy(C), IRB.getInt32Ty()}, false); 721 TestSetjmpF = getEmscriptenFunction(FTy, "testSetjmp", &M); 722 723 FTy = FunctionType::get(IRB.getVoidTy(), 724 {IRB.getInt32Ty(), IRB.getInt32Ty()}, false); 725 EmLongjmpF = getEmscriptenFunction(FTy, "emscripten_longjmp", &M); 726 727 // Only traverse functions that uses setjmp in order not to insert 728 // unnecessary prep / cleanup code in every function 729 SmallPtrSet<Function *, 8> SetjmpUsers; 730 for (User *U : SetjmpF->users()) { 731 auto *UI = cast<Instruction>(U); 732 SetjmpUsers.insert(UI->getFunction()); 733 } 734 for (Function *F : SetjmpUsers) 735 runSjLjOnFunction(*F); 736 } 737 } 738 739 if (!Changed) { 740 // Delete unused global variables and functions 741 if (ResumeF) 742 ResumeF->eraseFromParent(); 743 if (EHTypeIDF) 744 EHTypeIDF->eraseFromParent(); 745 if (EmLongjmpF) 746 EmLongjmpF->eraseFromParent(); 747 if (SaveSetjmpF) 748 SaveSetjmpF->eraseFromParent(); 749 if (TestSetjmpF) 750 TestSetjmpF->eraseFromParent(); 751 return false; 752 } 753 754 return true; 755 } 756 757 bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) { 758 Module &M = *F.getParent(); 759 LLVMContext &C = F.getContext(); 760 IRBuilder<> IRB(C); 761 bool Changed = false; 762 SmallVector<Instruction *, 64> ToErase; 763 SmallPtrSet<LandingPadInst *, 32> LandingPads; 764 bool AllowExceptions = areAllExceptionsAllowed() || 765 EHAllowlistSet.count(std::string(F.getName())); 766 767 for (BasicBlock &BB : F) { 768 auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); 769 if (!II) 770 continue; 771 Changed = true; 772 LandingPads.insert(II->getLandingPadInst()); 773 IRB.SetInsertPoint(II); 774 775 bool NeedInvoke = AllowExceptions && canThrow(II->getCalledOperand()); 776 if (NeedInvoke) { 777 // Wrap invoke with invoke wrapper and generate preamble/postamble 778 Value *Threw = wrapInvoke(II); 779 ToErase.push_back(II); 780 781 // Insert a branch based on __THREW__ variable 782 Value *Cmp = IRB.CreateICmpEQ(Threw, IRB.getInt32(1), "cmp"); 783 IRB.CreateCondBr(Cmp, II->getUnwindDest(), II->getNormalDest()); 784 785 } else { 786 // This can't throw, and we don't need this invoke, just replace it with a 787 // call+branch 788 SmallVector<Value *, 16> Args(II->arg_begin(), II->arg_end()); 789 CallInst *NewCall = 790 IRB.CreateCall(II->getFunctionType(), II->getCalledOperand(), Args); 791 NewCall->takeName(II); 792 NewCall->setCallingConv(II->getCallingConv()); 793 NewCall->setDebugLoc(II->getDebugLoc()); 794 NewCall->setAttributes(II->getAttributes()); 795 II->replaceAllUsesWith(NewCall); 796 ToErase.push_back(II); 797 798 IRB.CreateBr(II->getNormalDest()); 799 800 // Remove any PHI node entries from the exception destination 801 II->getUnwindDest()->removePredecessor(&BB); 802 } 803 } 804 805 // Process resume instructions 806 for (BasicBlock &BB : F) { 807 // Scan the body of the basic block for resumes 808 for (Instruction &I : BB) { 809 auto *RI = dyn_cast<ResumeInst>(&I); 810 if (!RI) 811 continue; 812 Changed = true; 813 814 // Split the input into legal values 815 Value *Input = RI->getValue(); 816 IRB.SetInsertPoint(RI); 817 Value *Low = IRB.CreateExtractValue(Input, 0, "low"); 818 // Create a call to __resumeException function 819 IRB.CreateCall(ResumeF, {Low}); 820 // Add a terminator to the block 821 IRB.CreateUnreachable(); 822 ToErase.push_back(RI); 823 } 824 } 825 826 // Process llvm.eh.typeid.for intrinsics 827 for (BasicBlock &BB : F) { 828 for (Instruction &I : BB) { 829 auto *CI = dyn_cast<CallInst>(&I); 830 if (!CI) 831 continue; 832 const Function *Callee = CI->getCalledFunction(); 833 if (!Callee) 834 continue; 835 if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for) 836 continue; 837 Changed = true; 838 839 IRB.SetInsertPoint(CI); 840 CallInst *NewCI = 841 IRB.CreateCall(EHTypeIDF, CI->getArgOperand(0), "typeid"); 842 CI->replaceAllUsesWith(NewCI); 843 ToErase.push_back(CI); 844 } 845 } 846 847 // Look for orphan landingpads, can occur in blocks with no predecessors 848 for (BasicBlock &BB : F) { 849 Instruction *I = BB.getFirstNonPHI(); 850 if (auto *LPI = dyn_cast<LandingPadInst>(I)) 851 LandingPads.insert(LPI); 852 } 853 Changed |= !LandingPads.empty(); 854 855 // Handle all the landingpad for this function together, as multiple invokes 856 // may share a single lp 857 for (LandingPadInst *LPI : LandingPads) { 858 IRB.SetInsertPoint(LPI); 859 SmallVector<Value *, 16> FMCArgs; 860 for (unsigned I = 0, E = LPI->getNumClauses(); I < E; ++I) { 861 Constant *Clause = LPI->getClause(I); 862 // As a temporary workaround for the lack of aggregate varargs support 863 // in the interface between JS and wasm, break out filter operands into 864 // their component elements. 865 if (LPI->isFilter(I)) { 866 auto *ATy = cast<ArrayType>(Clause->getType()); 867 for (unsigned J = 0, E = ATy->getNumElements(); J < E; ++J) { 868 Value *EV = IRB.CreateExtractValue(Clause, makeArrayRef(J), "filter"); 869 FMCArgs.push_back(EV); 870 } 871 } else 872 FMCArgs.push_back(Clause); 873 } 874 875 // Create a call to __cxa_find_matching_catch_N function 876 Function *FMCF = getFindMatchingCatch(M, FMCArgs.size()); 877 CallInst *FMCI = IRB.CreateCall(FMCF, FMCArgs, "fmc"); 878 Value *Undef = UndefValue::get(LPI->getType()); 879 Value *Pair0 = IRB.CreateInsertValue(Undef, FMCI, 0, "pair0"); 880 Value *TempRet0 = IRB.CreateCall(GetTempRet0Func, None, "tempret0"); 881 Value *Pair1 = IRB.CreateInsertValue(Pair0, TempRet0, 1, "pair1"); 882 883 LPI->replaceAllUsesWith(Pair1); 884 ToErase.push_back(LPI); 885 } 886 887 // Erase everything we no longer need in this function 888 for (Instruction *I : ToErase) 889 I->eraseFromParent(); 890 891 return Changed; 892 } 893 894 // This tries to get debug info from the instruction before which a new 895 // instruction will be inserted, and if there's no debug info in that 896 // instruction, tries to get the info instead from the previous instruction (if 897 // any). If none of these has debug info and a DISubprogram is provided, it 898 // creates a dummy debug info with the first line of the function, because IR 899 // verifier requires all inlinable callsites should have debug info when both a 900 // caller and callee have DISubprogram. If none of these conditions are met, 901 // returns empty info. 902 static DebugLoc getOrCreateDebugLoc(const Instruction *InsertBefore, 903 DISubprogram *SP) { 904 assert(InsertBefore); 905 if (InsertBefore->getDebugLoc()) 906 return InsertBefore->getDebugLoc(); 907 const Instruction *Prev = InsertBefore->getPrevNode(); 908 if (Prev && Prev->getDebugLoc()) 909 return Prev->getDebugLoc(); 910 if (SP) 911 return DILocation::get(SP->getContext(), SP->getLine(), 1, SP); 912 return DebugLoc(); 913 } 914 915 bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) { 916 Module &M = *F.getParent(); 917 LLVMContext &C = F.getContext(); 918 IRBuilder<> IRB(C); 919 SmallVector<Instruction *, 64> ToErase; 920 // Vector of %setjmpTable values 921 std::vector<Instruction *> SetjmpTableInsts; 922 // Vector of %setjmpTableSize values 923 std::vector<Instruction *> SetjmpTableSizeInsts; 924 925 // Setjmp preparation 926 927 // This instruction effectively means %setjmpTableSize = 4. 928 // We create this as an instruction intentionally, and we don't want to fold 929 // this instruction to a constant 4, because this value will be used in 930 // SSAUpdater.AddAvailableValue(...) later. 931 BasicBlock &EntryBB = F.getEntryBlock(); 932 DebugLoc FirstDL = getOrCreateDebugLoc(&*EntryBB.begin(), F.getSubprogram()); 933 BinaryOperator *SetjmpTableSize = BinaryOperator::Create( 934 Instruction::Add, IRB.getInt32(4), IRB.getInt32(0), "setjmpTableSize", 935 &*EntryBB.getFirstInsertionPt()); 936 SetjmpTableSize->setDebugLoc(FirstDL); 937 // setjmpTable = (int *) malloc(40); 938 Instruction *SetjmpTable = CallInst::CreateMalloc( 939 SetjmpTableSize, IRB.getInt32Ty(), IRB.getInt32Ty(), IRB.getInt32(40), 940 nullptr, nullptr, "setjmpTable"); 941 SetjmpTable->setDebugLoc(FirstDL); 942 // CallInst::CreateMalloc may return a bitcast instruction if the result types 943 // mismatch. We need to set the debug loc for the original call too. 944 auto *MallocCall = SetjmpTable->stripPointerCasts(); 945 if (auto *MallocCallI = dyn_cast<Instruction>(MallocCall)) { 946 MallocCallI->setDebugLoc(FirstDL); 947 } 948 // setjmpTable[0] = 0; 949 IRB.SetInsertPoint(SetjmpTableSize); 950 IRB.CreateStore(IRB.getInt32(0), SetjmpTable); 951 SetjmpTableInsts.push_back(SetjmpTable); 952 SetjmpTableSizeInsts.push_back(SetjmpTableSize); 953 954 // Setjmp transformation 955 std::vector<PHINode *> SetjmpRetPHIs; 956 Function *SetjmpF = M.getFunction("setjmp"); 957 for (User *U : SetjmpF->users()) { 958 auto *CI = dyn_cast<CallInst>(U); 959 if (!CI) 960 report_fatal_error("Does not support indirect calls to setjmp"); 961 962 BasicBlock *BB = CI->getParent(); 963 if (BB->getParent() != &F) // in other function 964 continue; 965 966 // The tail is everything right after the call, and will be reached once 967 // when setjmp is called, and later when longjmp returns to the setjmp 968 BasicBlock *Tail = SplitBlock(BB, CI->getNextNode()); 969 // Add a phi to the tail, which will be the output of setjmp, which 970 // indicates if this is the first call or a longjmp back. The phi directly 971 // uses the right value based on where we arrive from 972 IRB.SetInsertPoint(Tail->getFirstNonPHI()); 973 PHINode *SetjmpRet = IRB.CreatePHI(IRB.getInt32Ty(), 2, "setjmp.ret"); 974 975 // setjmp initial call returns 0 976 SetjmpRet->addIncoming(IRB.getInt32(0), BB); 977 // The proper output is now this, not the setjmp call itself 978 CI->replaceAllUsesWith(SetjmpRet); 979 // longjmp returns to the setjmp will add themselves to this phi 980 SetjmpRetPHIs.push_back(SetjmpRet); 981 982 // Fix call target 983 // Our index in the function is our place in the array + 1 to avoid index 984 // 0, because index 0 means the longjmp is not ours to handle. 985 IRB.SetInsertPoint(CI); 986 Value *Args[] = {CI->getArgOperand(0), IRB.getInt32(SetjmpRetPHIs.size()), 987 SetjmpTable, SetjmpTableSize}; 988 Instruction *NewSetjmpTable = 989 IRB.CreateCall(SaveSetjmpF, Args, "setjmpTable"); 990 Instruction *NewSetjmpTableSize = 991 IRB.CreateCall(GetTempRet0Func, None, "setjmpTableSize"); 992 SetjmpTableInsts.push_back(NewSetjmpTable); 993 SetjmpTableSizeInsts.push_back(NewSetjmpTableSize); 994 ToErase.push_back(CI); 995 } 996 997 // Update each call that can longjmp so it can return to a setjmp where 998 // relevant. 999 1000 // Because we are creating new BBs while processing and don't want to make 1001 // all these newly created BBs candidates again for longjmp processing, we 1002 // first make the vector of candidate BBs. 1003 std::vector<BasicBlock *> BBs; 1004 for (BasicBlock &BB : F) 1005 BBs.push_back(&BB); 1006 1007 // BBs.size() will change within the loop, so we query it every time 1008 for (unsigned I = 0; I < BBs.size(); I++) { 1009 BasicBlock *BB = BBs[I]; 1010 for (Instruction &I : *BB) { 1011 assert(!isa<InvokeInst>(&I)); 1012 auto *CI = dyn_cast<CallInst>(&I); 1013 if (!CI) 1014 continue; 1015 1016 const Value *Callee = CI->getCalledOperand(); 1017 if (!canLongjmp(M, Callee)) 1018 continue; 1019 if (isEmAsmCall(M, Callee)) 1020 report_fatal_error("Cannot use EM_ASM* alongside setjmp/longjmp in " + 1021 F.getName() + 1022 ". Please consider using EM_JS, or move the " 1023 "EM_ASM into another function.", 1024 false); 1025 1026 Value *Threw = nullptr; 1027 BasicBlock *Tail; 1028 if (Callee->getName().startswith("__invoke_")) { 1029 // If invoke wrapper has already been generated for this call in 1030 // previous EH phase, search for the load instruction 1031 // %__THREW__.val = __THREW__; 1032 // in postamble after the invoke wrapper call 1033 LoadInst *ThrewLI = nullptr; 1034 StoreInst *ThrewResetSI = nullptr; 1035 for (auto I = std::next(BasicBlock::iterator(CI)), IE = BB->end(); 1036 I != IE; ++I) { 1037 if (auto *LI = dyn_cast<LoadInst>(I)) 1038 if (auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand())) 1039 if (GV == ThrewGV) { 1040 Threw = ThrewLI = LI; 1041 break; 1042 } 1043 } 1044 // Search for the store instruction after the load above 1045 // __THREW__ = 0; 1046 for (auto I = std::next(BasicBlock::iterator(ThrewLI)), IE = BB->end(); 1047 I != IE; ++I) { 1048 if (auto *SI = dyn_cast<StoreInst>(I)) 1049 if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand())) 1050 if (GV == ThrewGV && SI->getValueOperand() == IRB.getInt32(0)) { 1051 ThrewResetSI = SI; 1052 break; 1053 } 1054 } 1055 assert(Threw && ThrewLI && "Cannot find __THREW__ load after invoke"); 1056 assert(ThrewResetSI && "Cannot find __THREW__ store after invoke"); 1057 Tail = SplitBlock(BB, ThrewResetSI->getNextNode()); 1058 1059 } else { 1060 // Wrap call with invoke wrapper and generate preamble/postamble 1061 Threw = wrapInvoke(CI); 1062 ToErase.push_back(CI); 1063 Tail = SplitBlock(BB, CI->getNextNode()); 1064 } 1065 1066 // We need to replace the terminator in Tail - SplitBlock makes BB go 1067 // straight to Tail, we need to check if a longjmp occurred, and go to the 1068 // right setjmp-tail if so 1069 ToErase.push_back(BB->getTerminator()); 1070 1071 // Generate a function call to testSetjmp function and preamble/postamble 1072 // code to figure out (1) whether longjmp occurred (2) if longjmp 1073 // occurred, which setjmp it corresponds to 1074 Value *Label = nullptr; 1075 Value *LongjmpResult = nullptr; 1076 BasicBlock *EndBB = nullptr; 1077 wrapTestSetjmp(BB, CI->getDebugLoc(), Threw, SetjmpTable, SetjmpTableSize, 1078 Label, LongjmpResult, EndBB); 1079 assert(Label && LongjmpResult && EndBB); 1080 1081 // Create switch instruction 1082 IRB.SetInsertPoint(EndBB); 1083 IRB.SetCurrentDebugLocation(EndBB->getInstList().back().getDebugLoc()); 1084 SwitchInst *SI = IRB.CreateSwitch(Label, Tail, SetjmpRetPHIs.size()); 1085 // -1 means no longjmp happened, continue normally (will hit the default 1086 // switch case). 0 means a longjmp that is not ours to handle, needs a 1087 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid 1088 // 0). 1089 for (unsigned I = 0; I < SetjmpRetPHIs.size(); I++) { 1090 SI->addCase(IRB.getInt32(I + 1), SetjmpRetPHIs[I]->getParent()); 1091 SetjmpRetPHIs[I]->addIncoming(LongjmpResult, EndBB); 1092 } 1093 1094 // We are splitting the block here, and must continue to find other calls 1095 // in the block - which is now split. so continue to traverse in the Tail 1096 BBs.push_back(Tail); 1097 } 1098 } 1099 1100 // Erase everything we no longer need in this function 1101 for (Instruction *I : ToErase) 1102 I->eraseFromParent(); 1103 1104 // Free setjmpTable buffer before each return instruction 1105 for (BasicBlock &BB : F) { 1106 Instruction *TI = BB.getTerminator(); 1107 if (isa<ReturnInst>(TI)) { 1108 DebugLoc DL = getOrCreateDebugLoc(TI, F.getSubprogram()); 1109 auto *Free = CallInst::CreateFree(SetjmpTable, TI); 1110 Free->setDebugLoc(DL); 1111 // CallInst::CreateFree may create a bitcast instruction if its argument 1112 // types mismatch. We need to set the debug loc for the bitcast too. 1113 if (auto *FreeCallI = dyn_cast<CallInst>(Free)) { 1114 if (auto *BitCastI = dyn_cast<BitCastInst>(FreeCallI->getArgOperand(0))) 1115 BitCastI->setDebugLoc(DL); 1116 } 1117 } 1118 } 1119 1120 // Every call to saveSetjmp can change setjmpTable and setjmpTableSize 1121 // (when buffer reallocation occurs) 1122 // entry: 1123 // setjmpTableSize = 4; 1124 // setjmpTable = (int *) malloc(40); 1125 // setjmpTable[0] = 0; 1126 // ... 1127 // somebb: 1128 // setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize); 1129 // setjmpTableSize = getTempRet0(); 1130 // So we need to make sure the SSA for these variables is valid so that every 1131 // saveSetjmp and testSetjmp calls have the correct arguments. 1132 SSAUpdater SetjmpTableSSA; 1133 SSAUpdater SetjmpTableSizeSSA; 1134 SetjmpTableSSA.Initialize(Type::getInt32PtrTy(C), "setjmpTable"); 1135 SetjmpTableSizeSSA.Initialize(Type::getInt32Ty(C), "setjmpTableSize"); 1136 for (Instruction *I : SetjmpTableInsts) 1137 SetjmpTableSSA.AddAvailableValue(I->getParent(), I); 1138 for (Instruction *I : SetjmpTableSizeInsts) 1139 SetjmpTableSizeSSA.AddAvailableValue(I->getParent(), I); 1140 1141 for (auto UI = SetjmpTable->use_begin(), UE = SetjmpTable->use_end(); 1142 UI != UE;) { 1143 // Grab the use before incrementing the iterator. 1144 Use &U = *UI; 1145 // Increment the iterator before removing the use from the list. 1146 ++UI; 1147 if (auto *I = dyn_cast<Instruction>(U.getUser())) 1148 if (I->getParent() != &EntryBB) 1149 SetjmpTableSSA.RewriteUse(U); 1150 } 1151 for (auto UI = SetjmpTableSize->use_begin(), UE = SetjmpTableSize->use_end(); 1152 UI != UE;) { 1153 Use &U = *UI; 1154 ++UI; 1155 if (auto *I = dyn_cast<Instruction>(U.getUser())) 1156 if (I->getParent() != &EntryBB) 1157 SetjmpTableSizeSSA.RewriteUse(U); 1158 } 1159 1160 // Finally, our modifications to the cfg can break dominance of SSA variables. 1161 // For example, in this code, 1162 // if (x()) { .. setjmp() .. } 1163 // if (y()) { .. longjmp() .. } 1164 // We must split the longjmp block, and it can jump into the block splitted 1165 // from setjmp one. But that means that when we split the setjmp block, it's 1166 // first part no longer dominates its second part - there is a theoretically 1167 // possible control flow path where x() is false, then y() is true and we 1168 // reach the second part of the setjmp block, without ever reaching the first 1169 // part. So, we rebuild SSA form here. 1170 rebuildSSA(F); 1171 return true; 1172 } 1173