Bug Summary

File:lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
Warning:line 666, column 31
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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