| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 1 | //===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // All functions using an MSVC EH personality use an explicitly updated state |
| 10 | // number stored in an exception registration stack object. The registration |
| 11 | // object is linked into a thread-local chain of registrations stored at fs:00. |
| 12 | // This pass adds the registration object and EH state updates. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "X86.h" |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/PostOrderIterator.h" |
| 18 | #include "llvm/Analysis/CFG.h" |
| David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/EHPersonalities.h" |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
| 23 | #include "llvm/IR/Function.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IRBuilder.h" |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Module.h" |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
| 30 | #include <deque> |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 33 | |
| 34 | #define DEBUG_TYPE "winehstate" |
| 35 | |
| 36 | namespace { |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 37 | const int OverdefinedState = INT_MIN; |
| 38 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 39 | class WinEHStatePass : public FunctionPass { |
| 40 | public: |
| 41 | static char ID; // Pass identification, replacement for typeid. |
| 42 | |
| Tom Stellard | f335672 | 2019-06-13 02:09:32 +0000 | [diff] [blame] | 43 | WinEHStatePass() : FunctionPass(ID) { } |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 44 | |
| 45 | bool runOnFunction(Function &Fn) override; |
| 46 | |
| 47 | bool doInitialization(Module &M) override; |
| 48 | |
| 49 | bool doFinalization(Module &M) override; |
| 50 | |
| 51 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 52 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 53 | StringRef getPassName() const override { |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 54 | return "Windows 32-bit x86 EH state insertion"; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | void emitExceptionRegistrationRecord(Function *F); |
| 59 | |
| Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 60 | void linkExceptionRegistration(IRBuilder<> &Builder, Function *Handler); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 61 | void unlinkExceptionRegistration(IRBuilder<> &Builder); |
| Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 62 | void addStateStores(Function &F, WinEHFuncInfo &FuncInfo); |
| David Majnemer | efb4174 | 2016-02-01 04:28:59 +0000 | [diff] [blame] | 63 | void insertStateNumberStore(Instruction *IP, int State); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 64 | |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 65 | Value *emitEHLSDA(IRBuilder<> &Builder, Function *F); |
| 66 | |
| 67 | Function *generateLSDAInEAXThunk(Function *ParentFunc); |
| 68 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 69 | bool isStateStoreNeeded(EHPersonality Personality, CallSite CS); |
| 70 | void rewriteSetJmpCallSite(IRBuilder<> &Builder, Function &F, CallSite CS, |
| 71 | Value *State); |
| 72 | int getBaseStateForBB(DenseMap<BasicBlock *, ColorVector> &BlockColors, |
| 73 | WinEHFuncInfo &FuncInfo, BasicBlock *BB); |
| 74 | int getStateForCallSite(DenseMap<BasicBlock *, ColorVector> &BlockColors, |
| 75 | WinEHFuncInfo &FuncInfo, CallSite CS); |
| 76 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 77 | // Module-level type getters. |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 78 | Type *getEHLinkRegistrationType(); |
| 79 | Type *getSEHRegistrationType(); |
| 80 | Type *getCXXEHRegistrationType(); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 81 | |
| 82 | // Per-module data. |
| 83 | Module *TheModule = nullptr; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 84 | StructType *EHLinkRegistrationTy = nullptr; |
| 85 | StructType *CXXEHRegistrationTy = nullptr; |
| 86 | StructType *SEHRegistrationTy = nullptr; |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 87 | FunctionCallee SetJmp3 = nullptr; |
| 88 | FunctionCallee CxxLongjmpUnwind = nullptr; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 89 | |
| 90 | // Per-function state |
| 91 | EHPersonality Personality = EHPersonality::Unknown; |
| 92 | Function *PersonalityFn = nullptr; |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 93 | bool UseStackGuard = false; |
| 94 | int ParentBaseState; |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 95 | FunctionCallee SehLongjmpUnwind = nullptr; |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 96 | Constant *Cookie = nullptr; |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 97 | |
| 98 | /// The stack allocation containing all EH data, including the link in the |
| 99 | /// fs:00 chain and the current state. |
| 100 | AllocaInst *RegNode = nullptr; |
| 101 | |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 102 | // The allocation containing the EH security guard. |
| 103 | AllocaInst *EHGuardNode = nullptr; |
| 104 | |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 105 | /// The index of the state field of RegNode. |
| 106 | int StateFieldIndex = ~0U; |
| 107 | |
| 108 | /// The linked list node subobject inside of RegNode. |
| 109 | Value *Link = nullptr; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 110 | }; |
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 111 | } |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 112 | |
| 113 | FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); } |
| 114 | |
| 115 | char WinEHStatePass::ID = 0; |
| 116 | |
| David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 117 | INITIALIZE_PASS(WinEHStatePass, "x86-winehstate", |
| 118 | "Insert stores for EH state numbers", false, false) |
| 119 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 120 | bool WinEHStatePass::doInitialization(Module &M) { |
| 121 | TheModule = &M; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | bool WinEHStatePass::doFinalization(Module &M) { |
| 126 | assert(TheModule == &M); |
| 127 | TheModule = nullptr; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 128 | EHLinkRegistrationTy = nullptr; |
| 129 | CXXEHRegistrationTy = nullptr; |
| 130 | SEHRegistrationTy = nullptr; |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 131 | SetJmp3 = nullptr; |
| 132 | CxxLongjmpUnwind = nullptr; |
| 133 | SehLongjmpUnwind = nullptr; |
| 134 | Cookie = nullptr; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 139 | // This pass should only insert a stack allocation, memory accesses, and |
| Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 140 | // localrecovers. |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 141 | AU.setPreservesCFG(); |
| 142 | } |
| 143 | |
| 144 | bool WinEHStatePass::runOnFunction(Function &F) { |
| Reid Kleckner | a2d119a | 2017-12-28 18:41:31 +0000 | [diff] [blame] | 145 | // Don't insert state stores or exception handler thunks for |
| 146 | // available_externally functions. The handler needs to reference the LSDA, |
| 147 | // which will not be emitted in this case. |
| 148 | if (F.hasAvailableExternallyLinkage()) |
| 149 | return false; |
| 150 | |
| Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 151 | // Check the personality. Do nothing if this personality doesn't use funclets. |
| David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 152 | if (!F.hasPersonalityFn()) |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 153 | return false; |
| 154 | PersonalityFn = |
| David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 155 | dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 156 | if (!PersonalityFn) |
| 157 | return false; |
| 158 | Personality = classifyEHPersonality(PersonalityFn); |
| Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 159 | if (!isFuncletEHPersonality(Personality)) |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 160 | return false; |
| 161 | |
| Reid Kleckner | 84ebff4 | 2015-09-16 17:19:44 +0000 | [diff] [blame] | 162 | // Skip this function if there are no EH pads and we aren't using IR-level |
| 163 | // outlining. |
| David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 164 | bool HasPads = false; |
| 165 | for (BasicBlock &BB : F) { |
| 166 | if (BB.isEHPad()) { |
| 167 | HasPads = true; |
| 168 | break; |
| Reid Kleckner | 84ebff4 | 2015-09-16 17:19:44 +0000 | [diff] [blame] | 169 | } |
| Reid Kleckner | 84ebff4 | 2015-09-16 17:19:44 +0000 | [diff] [blame] | 170 | } |
| David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 171 | if (!HasPads) |
| 172 | return false; |
| Reid Kleckner | 84ebff4 | 2015-09-16 17:19:44 +0000 | [diff] [blame] | 173 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 174 | Type *Int8PtrType = Type::getInt8PtrTy(TheModule->getContext()); |
| 175 | SetJmp3 = TheModule->getOrInsertFunction( |
| 176 | "_setjmp3", FunctionType::get( |
| 177 | Type::getInt32Ty(TheModule->getContext()), |
| 178 | {Int8PtrType, Type::getInt32Ty(TheModule->getContext())}, |
| 179 | /*isVarArg=*/true)); |
| David Majnemer | 862c5ba3 | 2016-02-20 07:34:21 +0000 | [diff] [blame] | 180 | |
| Reid Kleckner | 173a725 | 2015-05-29 21:58:11 +0000 | [diff] [blame] | 181 | // Disable frame pointer elimination in this function. |
| 182 | // FIXME: Do the nested handlers need to keep the parent ebp in ebp, or can we |
| 183 | // use an arbitrary register? |
| 184 | F.addFnAttr("no-frame-pointer-elim", "true"); |
| 185 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 186 | emitExceptionRegistrationRecord(&F); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 187 | |
| Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 188 | // The state numbers calculated here in IR must agree with what we calculate |
| 189 | // later on for the MachineFunction. In particular, if an IR pass deletes an |
| 190 | // unreachable EH pad after this point before machine CFG construction, we |
| 191 | // will be in trouble. If this assumption is ever broken, we should turn the |
| 192 | // numbers into an immutable analysis pass. |
| 193 | WinEHFuncInfo FuncInfo; |
| 194 | addStateStores(F, FuncInfo); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 195 | |
| 196 | // Reset per-function state. |
| 197 | PersonalityFn = nullptr; |
| 198 | Personality = EHPersonality::Unknown; |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 199 | UseStackGuard = false; |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 200 | RegNode = nullptr; |
| 201 | EHGuardNode = nullptr; |
| 202 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | |
| 206 | /// Get the common EH registration subobject: |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 207 | /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)( |
| 208 | /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 209 | /// struct EHRegistrationNode { |
| 210 | /// EHRegistrationNode *Next; |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 211 | /// PEXCEPTION_ROUTINE Handler; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 212 | /// }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 213 | Type *WinEHStatePass::getEHLinkRegistrationType() { |
| 214 | if (EHLinkRegistrationTy) |
| 215 | return EHLinkRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 216 | LLVMContext &Context = TheModule->getContext(); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 217 | EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode"); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 218 | Type *FieldTys[] = { |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 219 | EHLinkRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 220 | Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...) |
| 221 | }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 222 | EHLinkRegistrationTy->setBody(FieldTys, false); |
| 223 | return EHLinkRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /// The __CxxFrameHandler3 registration node: |
| 227 | /// struct CXXExceptionRegistration { |
| 228 | /// void *SavedESP; |
| 229 | /// EHRegistrationNode SubRecord; |
| 230 | /// int32_t TryLevel; |
| 231 | /// }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 232 | Type *WinEHStatePass::getCXXEHRegistrationType() { |
| 233 | if (CXXEHRegistrationTy) |
| 234 | return CXXEHRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 235 | LLVMContext &Context = TheModule->getContext(); |
| 236 | Type *FieldTys[] = { |
| 237 | Type::getInt8PtrTy(Context), // void *SavedESP |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 238 | getEHLinkRegistrationType(), // EHRegistrationNode SubRecord |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 239 | Type::getInt32Ty(Context) // int32_t TryLevel |
| 240 | }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 241 | CXXEHRegistrationTy = |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 242 | StructType::create(FieldTys, "CXXExceptionRegistration"); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 243 | return CXXEHRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 246 | /// The _except_handler3/4 registration node: |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 247 | /// struct EH4ExceptionRegistration { |
| 248 | /// void *SavedESP; |
| 249 | /// _EXCEPTION_POINTERS *ExceptionPointers; |
| 250 | /// EHRegistrationNode SubRecord; |
| 251 | /// int32_t EncodedScopeTable; |
| 252 | /// int32_t TryLevel; |
| 253 | /// }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 254 | Type *WinEHStatePass::getSEHRegistrationType() { |
| 255 | if (SEHRegistrationTy) |
| 256 | return SEHRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 257 | LLVMContext &Context = TheModule->getContext(); |
| 258 | Type *FieldTys[] = { |
| 259 | Type::getInt8PtrTy(Context), // void *SavedESP |
| 260 | Type::getInt8PtrTy(Context), // void *ExceptionPointers |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 261 | getEHLinkRegistrationType(), // EHRegistrationNode SubRecord |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 262 | Type::getInt32Ty(Context), // int32_t EncodedScopeTable |
| 263 | Type::getInt32Ty(Context) // int32_t TryLevel |
| 264 | }; |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 265 | SEHRegistrationTy = StructType::create(FieldTys, "SEHExceptionRegistration"); |
| 266 | return SEHRegistrationTy; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Emit an exception registration record. These are stack allocations with the |
| 270 | // common subobject of two pointers: the previous registration record (the old |
| 271 | // fs:00) and the personality function for the current frame. The data before |
| 272 | // and after that is personality function specific. |
| 273 | void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) { |
| 274 | assert(Personality == EHPersonality::MSVC_CXX || |
| 275 | Personality == EHPersonality::MSVC_X86SEH); |
| 276 | |
| David Majnemer | efb4174 | 2016-02-01 04:28:59 +0000 | [diff] [blame] | 277 | // Struct type of RegNode. Used for GEPing. |
| 278 | Type *RegNodeTy; |
| 279 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 280 | IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin()); |
| 281 | Type *Int8PtrType = Builder.getInt8PtrTy(); |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 282 | Type *Int32Ty = Builder.getInt32Ty(); |
| 283 | Type *VoidTy = Builder.getVoidTy(); |
| 284 | |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 285 | if (Personality == EHPersonality::MSVC_CXX) { |
| 286 | RegNodeTy = getCXXEHRegistrationType(); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 287 | RegNode = Builder.CreateAlloca(RegNodeTy); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 288 | // SavedESP = llvm.stacksave() |
| 289 | Value *SP = Builder.CreateCall( |
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 290 | Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {}); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 291 | Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0)); |
| 292 | // TryLevel = -1 |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 293 | StateFieldIndex = 2; |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 294 | ParentBaseState = -1; |
| 295 | insertStateNumberStore(&*Builder.GetInsertPoint(), ParentBaseState); |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 296 | // Handler = __ehhandler$F |
| 297 | Function *Trampoline = generateLSDAInEAXThunk(F); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 298 | Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 1); |
| 299 | linkExceptionRegistration(Builder, Trampoline); |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 300 | |
| 301 | CxxLongjmpUnwind = TheModule->getOrInsertFunction( |
| 302 | "__CxxLongjmpUnwind", |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 303 | FunctionType::get(VoidTy, Int8PtrType, /*isVarArg=*/false)); |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 304 | cast<Function>(CxxLongjmpUnwind.getCallee()->stripPointerCasts()) |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 305 | ->setCallingConv(CallingConv::X86_StdCall); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 306 | } else if (Personality == EHPersonality::MSVC_X86SEH) { |
| 307 | // If _except_handler4 is in use, some additional guard checks and prologue |
| 308 | // stuff is required. |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 309 | StringRef PersonalityName = PersonalityFn->getName(); |
| 310 | UseStackGuard = (PersonalityName == "_except_handler4"); |
| 311 | |
| 312 | // Allocate local structures. |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 313 | RegNodeTy = getSEHRegistrationType(); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 314 | RegNode = Builder.CreateAlloca(RegNodeTy); |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 315 | if (UseStackGuard) |
| 316 | EHGuardNode = Builder.CreateAlloca(Int32Ty); |
| 317 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 318 | // SavedESP = llvm.stacksave() |
| 319 | Value *SP = Builder.CreateCall( |
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 320 | Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {}); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 321 | Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0)); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 322 | // TryLevel = -2 / -1 |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 323 | StateFieldIndex = 4; |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 324 | ParentBaseState = UseStackGuard ? -2 : -1; |
| 325 | insertStateNumberStore(&*Builder.GetInsertPoint(), ParentBaseState); |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 326 | // ScopeTable = llvm.x86.seh.lsda(F) |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 327 | Value *LSDA = emitEHLSDA(Builder, F); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 328 | LSDA = Builder.CreatePtrToInt(LSDA, Int32Ty); |
| 329 | // If using _except_handler4, xor the address of the table with |
| 330 | // __security_cookie. |
| 331 | if (UseStackGuard) { |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 332 | Cookie = TheModule->getOrInsertGlobal("__security_cookie", Int32Ty); |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 333 | Value *Val = Builder.CreateLoad(Int32Ty, Cookie, "cookie"); |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 334 | LSDA = Builder.CreateXor(LSDA, Val); |
| 335 | } |
| 336 | Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 3)); |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 337 | |
| 338 | // If using _except_handler4, the EHGuard contains: FramePtr xor Cookie. |
| 339 | if (UseStackGuard) { |
| 340 | Value *Val = Builder.CreateLoad(Int32Ty, Cookie); |
| 341 | Value *FrameAddr = Builder.CreateCall( |
| Christudasan Devadasan | 006cf8c | 2019-07-22 12:42:48 +0000 | [diff] [blame] | 342 | Intrinsic::getDeclaration( |
| 343 | TheModule, Intrinsic::frameaddress, |
| 344 | Builder.getInt8PtrTy( |
| 345 | TheModule->getDataLayout().getAllocaAddrSpace())), |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 346 | Builder.getInt32(0), "frameaddr"); |
| 347 | Value *FrameAddrI32 = Builder.CreatePtrToInt(FrameAddr, Int32Ty); |
| 348 | FrameAddrI32 = Builder.CreateXor(FrameAddrI32, Val); |
| 349 | Builder.CreateStore(FrameAddrI32, EHGuardNode); |
| 350 | } |
| 351 | |
| 352 | // Register the exception handler. |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 353 | Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2); |
| 354 | linkExceptionRegistration(Builder, PersonalityFn); |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 355 | |
| 356 | SehLongjmpUnwind = TheModule->getOrInsertFunction( |
| 357 | UseStackGuard ? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind", |
| 358 | FunctionType::get(Type::getVoidTy(TheModule->getContext()), Int8PtrType, |
| 359 | /*isVarArg=*/false)); |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 360 | cast<Function>(SehLongjmpUnwind.getCallee()->stripPointerCasts()) |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 361 | ->setCallingConv(CallingConv::X86_StdCall); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 362 | } else { |
| 363 | llvm_unreachable("unexpected personality function"); |
| 364 | } |
| 365 | |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 366 | // Insert an unlink before all returns. |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 367 | for (BasicBlock &BB : *F) { |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 368 | Instruction *T = BB.getTerminator(); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 369 | if (!isa<ReturnInst>(T)) |
| 370 | continue; |
| 371 | Builder.SetInsertPoint(T); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 372 | unlinkExceptionRegistration(Builder); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 376 | Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) { |
| 377 | Value *FI8 = Builder.CreateBitCast(F, Type::getInt8PtrTy(F->getContext())); |
| 378 | return Builder.CreateCall( |
| 379 | Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8); |
| 380 | } |
| 381 | |
| 382 | /// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls |
| 383 | /// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE: |
| 384 | /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)( |
| 385 | /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *); |
| 386 | /// We essentially want this code: |
| 387 | /// movl $lsda, %eax |
| 388 | /// jmpl ___CxxFrameHandler3 |
| 389 | Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) { |
| 390 | LLVMContext &Context = ParentFunc->getContext(); |
| 391 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 392 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
| 393 | Type *ArgTys[5] = {Int8PtrType, Int8PtrType, Int8PtrType, Int8PtrType, |
| 394 | Int8PtrType}; |
| 395 | FunctionType *TrampolineTy = |
| 396 | FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 4), |
| 397 | /*isVarArg=*/false); |
| 398 | FunctionType *TargetFuncTy = |
| 399 | FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 5), |
| 400 | /*isVarArg=*/false); |
| Reid Kleckner | 5f4dd92 | 2015-07-13 17:55:14 +0000 | [diff] [blame] | 401 | Function *Trampoline = |
| 402 | Function::Create(TrampolineTy, GlobalValue::InternalLinkage, |
| Peter Collingbourne | 6f0ecca | 2017-05-16 00:39:01 +0000 | [diff] [blame] | 403 | Twine("__ehhandler$") + GlobalValue::dropLLVMManglingEscape( |
| Reid Kleckner | 5f4dd92 | 2015-07-13 17:55:14 +0000 | [diff] [blame] | 404 | ParentFunc->getName()), |
| 405 | TheModule); |
| Dave Lee | f9b7232 | 2017-10-20 17:04:43 +0000 | [diff] [blame] | 406 | if (auto *C = ParentFunc->getComdat()) |
| 407 | Trampoline->setComdat(C); |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 408 | BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline); |
| 409 | IRBuilder<> Builder(EntryBB); |
| 410 | Value *LSDA = emitEHLSDA(Builder, ParentFunc); |
| 411 | Value *CastPersonality = |
| 412 | Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo()); |
| 413 | auto AI = Trampoline->arg_begin(); |
| Duncan P. N. Exon Smith | d77de64 | 2015-10-19 21:48:29 +0000 | [diff] [blame] | 414 | Value *Args[5] = {LSDA, &*AI++, &*AI++, &*AI++, &*AI++}; |
| James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 415 | CallInst *Call = Builder.CreateCall(TargetFuncTy, CastPersonality, Args); |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 416 | // Can't use musttail due to prototype mismatch, but we can use tail. |
| 417 | Call->setTailCall(true); |
| 418 | // Set inreg so we pass it in EAX. |
| Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 419 | Call->addParamAttr(0, Attribute::InReg); |
| Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 420 | Builder.CreateRet(Call); |
| 421 | return Trampoline; |
| 422 | } |
| 423 | |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 424 | void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder, |
| Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 425 | Function *Handler) { |
| 426 | // Emit the .safeseh directive for this function. |
| 427 | Handler->addFnAttr("safeseh"); |
| 428 | |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 429 | Type *LinkTy = getEHLinkRegistrationType(); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 430 | // Handler = Handler |
| Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 431 | Value *HandlerI8 = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy()); |
| 432 | Builder.CreateStore(HandlerI8, Builder.CreateStructGEP(LinkTy, Link, 1)); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 433 | // Next = [fs:00] |
| 434 | Constant *FSZero = |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 435 | Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); |
| James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 436 | Value *Next = Builder.CreateLoad(LinkTy->getPointerTo(), FSZero); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 437 | Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0)); |
| 438 | // [fs:00] = Link |
| 439 | Builder.CreateStore(Link, FSZero); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 442 | void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) { |
| 443 | // Clone Link into the current BB for better address mode folding. |
| 444 | if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) { |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 445 | GEP = cast<GetElementPtrInst>(GEP->clone()); |
| 446 | Builder.Insert(GEP); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 447 | Link = GEP; |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 448 | } |
| Reid Kleckner | e6531a55 | 2015-05-29 22:57:46 +0000 | [diff] [blame] | 449 | Type *LinkTy = getEHLinkRegistrationType(); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 450 | // [fs:00] = Link->Next |
| James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 451 | Value *Next = Builder.CreateLoad(LinkTy->getPointerTo(), |
| 452 | Builder.CreateStructGEP(LinkTy, Link, 0)); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 453 | Constant *FSZero = |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 454 | Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); |
| Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 455 | Builder.CreateStore(Next, FSZero); |
| 456 | } |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 457 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 458 | // Calls to setjmp(p) are lowered to _setjmp3(p, 0) by the frontend. |
| 459 | // The idea behind _setjmp3 is that it takes an optional number of personality |
| 460 | // specific parameters to indicate how to restore the personality-specific frame |
| 461 | // state when longjmp is initiated. Typically, the current TryLevel is saved. |
| 462 | void WinEHStatePass::rewriteSetJmpCallSite(IRBuilder<> &Builder, Function &F, |
| 463 | CallSite CS, Value *State) { |
| 464 | // Don't rewrite calls with a weird number of arguments. |
| 465 | if (CS.getNumArgOperands() != 2) |
| 466 | return; |
| 467 | |
| 468 | Instruction *Inst = CS.getInstruction(); |
| 469 | |
| 470 | SmallVector<OperandBundleDef, 1> OpBundles; |
| 471 | CS.getOperandBundlesAsDefs(OpBundles); |
| 472 | |
| 473 | SmallVector<Value *, 3> OptionalArgs; |
| 474 | if (Personality == EHPersonality::MSVC_CXX) { |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 475 | OptionalArgs.push_back(CxxLongjmpUnwind.getCallee()); |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 476 | OptionalArgs.push_back(State); |
| 477 | OptionalArgs.push_back(emitEHLSDA(Builder, &F)); |
| 478 | } else if (Personality == EHPersonality::MSVC_X86SEH) { |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 479 | OptionalArgs.push_back(SehLongjmpUnwind.getCallee()); |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 480 | OptionalArgs.push_back(State); |
| 481 | if (UseStackGuard) |
| 482 | OptionalArgs.push_back(Cookie); |
| 483 | } else { |
| 484 | llvm_unreachable("unhandled personality!"); |
| 485 | } |
| 486 | |
| 487 | SmallVector<Value *, 5> Args; |
| 488 | Args.push_back( |
| 489 | Builder.CreateBitCast(CS.getArgOperand(0), Builder.getInt8PtrTy())); |
| 490 | Args.push_back(Builder.getInt32(OptionalArgs.size())); |
| 491 | Args.append(OptionalArgs.begin(), OptionalArgs.end()); |
| 492 | |
| 493 | CallSite NewCS; |
| 494 | if (CS.isCall()) { |
| 495 | auto *CI = cast<CallInst>(Inst); |
| 496 | CallInst *NewCI = Builder.CreateCall(SetJmp3, Args, OpBundles); |
| 497 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 498 | NewCS = NewCI; |
| 499 | } else { |
| 500 | auto *II = cast<InvokeInst>(Inst); |
| 501 | NewCS = Builder.CreateInvoke( |
| 502 | SetJmp3, II->getNormalDest(), II->getUnwindDest(), Args, OpBundles); |
| 503 | } |
| 504 | NewCS.setCallingConv(CS.getCallingConv()); |
| 505 | NewCS.setAttributes(CS.getAttributes()); |
| 506 | NewCS->setDebugLoc(CS->getDebugLoc()); |
| 507 | |
| 508 | Instruction *NewInst = NewCS.getInstruction(); |
| 509 | NewInst->takeName(Inst); |
| 510 | Inst->replaceAllUsesWith(NewInst); |
| 511 | Inst->eraseFromParent(); |
| 512 | } |
| 513 | |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 514 | // Figure out what state we should assign calls in this block. |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 515 | int WinEHStatePass::getBaseStateForBB( |
| 516 | DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo, |
| 517 | BasicBlock *BB) { |
| 518 | int BaseState = ParentBaseState; |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 519 | auto &BBColors = BlockColors[BB]; |
| 520 | |
| 521 | assert(BBColors.size() == 1 && "multi-color BB not removed by preparation"); |
| 522 | BasicBlock *FuncletEntryBB = BBColors.front(); |
| 523 | if (auto *FuncletPad = |
| 524 | dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI())) { |
| 525 | auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad); |
| 526 | if (BaseStateI != FuncInfo.FuncletBaseStateMap.end()) |
| 527 | BaseState = BaseStateI->second; |
| 528 | } |
| 529 | |
| 530 | return BaseState; |
| 531 | } |
| 532 | |
| 533 | // Calculate the state a call-site is in. |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 534 | int WinEHStatePass::getStateForCallSite( |
| 535 | DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo, |
| 536 | CallSite CS) { |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 537 | if (auto *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 538 | // Look up the state number of the EH pad this unwinds to. |
| 539 | assert(FuncInfo.InvokeStateMap.count(II) && "invoke has no state!"); |
| 540 | return FuncInfo.InvokeStateMap[II]; |
| 541 | } |
| 542 | // Possibly throwing call instructions have no actions to take after |
| 543 | // an unwind. Ensure they are in the -1 state. |
| 544 | return getBaseStateForBB(BlockColors, FuncInfo, CS.getParent()); |
| 545 | } |
| 546 | |
| 547 | // Calculate the intersection of all the FinalStates for a BasicBlock's |
| David Majnemer | a822c88 | 2016-02-18 21:13:35 +0000 | [diff] [blame] | 548 | // predecessors. |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 549 | static int getPredState(DenseMap<BasicBlock *, int> &FinalStates, Function &F, |
| 550 | int ParentBaseState, BasicBlock *BB) { |
| 551 | // The entry block has no predecessors but we know that the prologue always |
| 552 | // sets us up with a fixed state. |
| 553 | if (&F.getEntryBlock() == BB) |
| 554 | return ParentBaseState; |
| 555 | |
| 556 | // This is an EH Pad, conservatively report this basic block as overdefined. |
| 557 | if (BB->isEHPad()) |
| 558 | return OverdefinedState; |
| 559 | |
| 560 | int CommonState = OverdefinedState; |
| 561 | for (BasicBlock *PredBB : predecessors(BB)) { |
| 562 | // We didn't manage to get a state for one of these predecessors, |
| 563 | // conservatively report this basic block as overdefined. |
| 564 | auto PredEndState = FinalStates.find(PredBB); |
| 565 | if (PredEndState == FinalStates.end()) |
| 566 | return OverdefinedState; |
| 567 | |
| 568 | // This code is reachable via exceptional control flow, |
| 569 | // conservatively report this basic block as overdefined. |
| 570 | if (isa<CatchReturnInst>(PredBB->getTerminator())) |
| 571 | return OverdefinedState; |
| 572 | |
| 573 | int PredState = PredEndState->second; |
| 574 | assert(PredState != OverdefinedState && |
| 575 | "overdefined BBs shouldn't be in FinalStates"); |
| 576 | if (CommonState == OverdefinedState) |
| 577 | CommonState = PredState; |
| 578 | |
| 579 | // At least two predecessors have different FinalStates, |
| 580 | // conservatively report this basic block as overdefined. |
| 581 | if (CommonState != PredState) |
| 582 | return OverdefinedState; |
| 583 | } |
| 584 | |
| 585 | return CommonState; |
| Nico Weber | 32ac273 | 2016-02-17 18:48:08 +0000 | [diff] [blame] | 586 | } |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 587 | |
| David Majnemer | a822c88 | 2016-02-18 21:13:35 +0000 | [diff] [blame] | 588 | // Calculate the intersection of all the InitialStates for a BasicBlock's |
| 589 | // successors. |
| 590 | static int getSuccState(DenseMap<BasicBlock *, int> &InitialStates, Function &F, |
| 591 | int ParentBaseState, BasicBlock *BB) { |
| 592 | // This block rejoins normal control flow, |
| 593 | // conservatively report this basic block as overdefined. |
| 594 | if (isa<CatchReturnInst>(BB->getTerminator())) |
| 595 | return OverdefinedState; |
| 596 | |
| 597 | int CommonState = OverdefinedState; |
| 598 | for (BasicBlock *SuccBB : successors(BB)) { |
| 599 | // We didn't manage to get a state for one of these predecessors, |
| 600 | // conservatively report this basic block as overdefined. |
| 601 | auto SuccStartState = InitialStates.find(SuccBB); |
| 602 | if (SuccStartState == InitialStates.end()) |
| 603 | return OverdefinedState; |
| 604 | |
| 605 | // This is an EH Pad, conservatively report this basic block as overdefined. |
| 606 | if (SuccBB->isEHPad()) |
| 607 | return OverdefinedState; |
| 608 | |
| 609 | int SuccState = SuccStartState->second; |
| 610 | assert(SuccState != OverdefinedState && |
| 611 | "overdefined BBs shouldn't be in FinalStates"); |
| 612 | if (CommonState == OverdefinedState) |
| 613 | CommonState = SuccState; |
| 614 | |
| 615 | // At least two successors have different InitialStates, |
| 616 | // conservatively report this basic block as overdefined. |
| 617 | if (CommonState != SuccState) |
| 618 | return OverdefinedState; |
| 619 | } |
| 620 | |
| 621 | return CommonState; |
| 622 | } |
| 623 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 624 | bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality, |
| 625 | CallSite CS) { |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 626 | if (!CS) |
| 627 | return false; |
| 628 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 629 | // If the function touches memory, it needs a state store. |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 630 | if (isAsynchronousEHPersonality(Personality)) |
| 631 | return !CS.doesNotAccessMemory(); |
| 632 | |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 633 | // If the function throws, it needs a state store. |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 634 | return !CS.doesNotThrow(); |
| 635 | } |
| 636 | |
| Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 637 | void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) { |
| 638 | // Mark the registration node. The backend needs to know which alloca it is so |
| 639 | // that it can recover the original frame pointer. |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 640 | IRBuilder<> Builder(RegNode->getNextNode()); |
| Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 641 | Value *RegNodeI8 = Builder.CreateBitCast(RegNode, Builder.getInt8PtrTy()); |
| 642 | Builder.CreateCall( |
| 643 | Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehregnode), |
| 644 | {RegNodeI8}); |
| Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 645 | |
| Etienne Bergeron | f6be62f | 2016-06-21 15:58:55 +0000 | [diff] [blame] | 646 | if (EHGuardNode) { |
| 647 | IRBuilder<> Builder(EHGuardNode->getNextNode()); |
| 648 | Value *EHGuardNodeI8 = |
| 649 | Builder.CreateBitCast(EHGuardNode, Builder.getInt8PtrTy()); |
| 650 | Builder.CreateCall( |
| 651 | Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehguard), |
| 652 | {EHGuardNodeI8}); |
| 653 | } |
| 654 | |
| Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 655 | // Calculate state numbers. |
| 656 | if (isAsynchronousEHPersonality(Personality)) |
| 657 | calculateSEHStateNumbers(&F, FuncInfo); |
| 658 | else |
| 659 | calculateWinCXXEHStateNumbers(&F, FuncInfo); |
| Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 660 | |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 661 | // Iterate all the instructions and emit state number stores. |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 662 | DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(F); |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 663 | ReversePostOrderTraversal<Function *> RPOT(&F); |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 664 | |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 665 | // InitialStates yields the state of the first call-site for a BasicBlock. |
| 666 | DenseMap<BasicBlock *, int> InitialStates; |
| 667 | // FinalStates yields the state of the last call-site for a BasicBlock. |
| 668 | DenseMap<BasicBlock *, int> FinalStates; |
| 669 | // Worklist used to revisit BasicBlocks with indeterminate |
| 670 | // Initial/Final-States. |
| 671 | std::deque<BasicBlock *> Worklist; |
| 672 | // Fill in InitialStates and FinalStates for BasicBlocks with call-sites. |
| 673 | for (BasicBlock *BB : RPOT) { |
| 674 | int InitialState = OverdefinedState; |
| 675 | int FinalState; |
| 676 | if (&F.getEntryBlock() == BB) |
| 677 | InitialState = FinalState = ParentBaseState; |
| 678 | for (Instruction &I : *BB) { |
| 679 | CallSite CS(&I); |
| 680 | if (!isStateStoreNeeded(Personality, CS)) |
| David Majnemer | f2bb710 | 2016-01-29 05:33:15 +0000 | [diff] [blame] | 681 | continue; |
| 682 | |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 683 | int State = getStateForCallSite(BlockColors, FuncInfo, CS); |
| 684 | if (InitialState == OverdefinedState) |
| 685 | InitialState = State; |
| 686 | FinalState = State; |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 687 | } |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 688 | // No call-sites in this basic block? That's OK, we will come back to these |
| 689 | // in a later pass. |
| 690 | if (InitialState == OverdefinedState) { |
| 691 | Worklist.push_back(BB); |
| 692 | continue; |
| 693 | } |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 694 | LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB->getName() |
| 695 | << " InitialState=" << InitialState << '\n'); |
| 696 | LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB->getName() |
| 697 | << " FinalState=" << FinalState << '\n'); |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 698 | InitialStates.insert({BB, InitialState}); |
| 699 | FinalStates.insert({BB, FinalState}); |
| 700 | } |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 701 | |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 702 | // Try to fill-in InitialStates and FinalStates which have no call-sites. |
| 703 | while (!Worklist.empty()) { |
| 704 | BasicBlock *BB = Worklist.front(); |
| 705 | Worklist.pop_front(); |
| 706 | // This BasicBlock has already been figured out, nothing more we can do. |
| 707 | if (InitialStates.count(BB) != 0) |
| 708 | continue; |
| 709 | |
| 710 | int PredState = getPredState(FinalStates, F, ParentBaseState, BB); |
| 711 | if (PredState == OverdefinedState) |
| 712 | continue; |
| 713 | |
| 714 | // We successfully inferred this BasicBlock's state via it's predecessors; |
| 715 | // enqueue it's successors to see if we can infer their states. |
| 716 | InitialStates.insert({BB, PredState}); |
| 717 | FinalStates.insert({BB, PredState}); |
| 718 | for (BasicBlock *SuccBB : successors(BB)) |
| 719 | Worklist.push_back(SuccBB); |
| 720 | } |
| 721 | |
| David Majnemer | a822c88 | 2016-02-18 21:13:35 +0000 | [diff] [blame] | 722 | // Try to hoist stores from successors. |
| 723 | for (BasicBlock *BB : RPOT) { |
| 724 | int SuccState = getSuccState(InitialStates, F, ParentBaseState, BB); |
| 725 | if (SuccState == OverdefinedState) |
| 726 | continue; |
| 727 | |
| 728 | // Update our FinalState to reflect the common InitialState of our |
| 729 | // successors. |
| 730 | FinalStates.insert({BB, SuccState}); |
| 731 | } |
| 732 | |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 733 | // Finally, insert state stores before call-sites which transition us to a new |
| 734 | // state. |
| 735 | for (BasicBlock *BB : RPOT) { |
| 736 | auto &BBColors = BlockColors[BB]; |
| 737 | BasicBlock *FuncletEntryBB = BBColors.front(); |
| 738 | if (isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI())) |
| 739 | continue; |
| 740 | |
| 741 | int PrevState = getPredState(FinalStates, F, ParentBaseState, BB); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 742 | LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB->getName() |
| 743 | << " PrevState=" << PrevState << '\n'); |
| David Majnemer | 7e5937b | 2016-02-17 18:37:11 +0000 | [diff] [blame] | 744 | |
| 745 | for (Instruction &I : *BB) { |
| 746 | CallSite CS(&I); |
| 747 | if (!isStateStoreNeeded(Personality, CS)) |
| 748 | continue; |
| 749 | |
| 750 | int State = getStateForCallSite(BlockColors, FuncInfo, CS); |
| 751 | if (State != PrevState) |
| 752 | insertStateNumberStore(&I, State); |
| 753 | PrevState = State; |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 754 | } |
| David Majnemer | a822c88 | 2016-02-18 21:13:35 +0000 | [diff] [blame] | 755 | |
| 756 | // We might have hoisted a state store into this block, emit it now. |
| 757 | auto EndState = FinalStates.find(BB); |
| 758 | if (EndState != FinalStates.end()) |
| 759 | if (EndState->second != PrevState) |
| 760 | insertStateNumberStore(BB->getTerminator(), EndState->second); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 761 | } |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 762 | |
| 763 | SmallVector<CallSite, 1> SetJmp3CallSites; |
| 764 | for (BasicBlock *BB : RPOT) { |
| 765 | for (Instruction &I : *BB) { |
| 766 | CallSite CS(&I); |
| 767 | if (!CS) |
| 768 | continue; |
| 769 | if (CS.getCalledValue()->stripPointerCasts() != |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 770 | SetJmp3.getCallee()->stripPointerCasts()) |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 771 | continue; |
| 772 | |
| 773 | SetJmp3CallSites.push_back(CS); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | for (CallSite CS : SetJmp3CallSites) { |
| 778 | auto &BBColors = BlockColors[CS->getParent()]; |
| 779 | BasicBlock *FuncletEntryBB = BBColors.front(); |
| 780 | bool InCleanup = isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI()); |
| 781 | |
| 782 | IRBuilder<> Builder(CS.getInstruction()); |
| 783 | Value *State; |
| 784 | if (InCleanup) { |
| James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 785 | Value *StateField = Builder.CreateStructGEP(RegNode->getAllocatedType(), |
| 786 | RegNode, StateFieldIndex); |
| James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 787 | State = Builder.CreateLoad(Builder.getInt32Ty(), StateField); |
| David Majnemer | e60ee3b | 2016-02-29 19:16:03 +0000 | [diff] [blame] | 788 | } else { |
| 789 | State = Builder.getInt32(getStateForCallSite(BlockColors, FuncInfo, CS)); |
| 790 | } |
| 791 | rewriteSetJmpCallSite(Builder, F, CS, State); |
| 792 | } |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| David Majnemer | efb4174 | 2016-02-01 04:28:59 +0000 | [diff] [blame] | 795 | void WinEHStatePass::insertStateNumberStore(Instruction *IP, int State) { |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 796 | IRBuilder<> Builder(IP); |
| James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 797 | Value *StateField = Builder.CreateStructGEP(RegNode->getAllocatedType(), |
| 798 | RegNode, StateFieldIndex); |
| Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 799 | Builder.CreateStore(Builder.getInt32(State), StateField); |
| 800 | } |