Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 1 | //===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===// |
| 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 | // All functions using an MSVC EH personality use an explicitly updated state |
| 11 | // number stored in an exception registration stack object. The registration |
| 12 | // object is linked into a thread-local chain of registrations stored at fs:00. |
| 13 | // This pass adds the registration object and EH state updates. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "X86.h" |
| 18 | #include "llvm/Analysis/LibCallSemantics.h" |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
| 22 | #include "llvm/IR/Dominators.h" |
| 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/IRBuilder.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Module.h" |
| 28 | #include "llvm/IR/PatternMatch.h" |
| 29 | #include "llvm/Pass.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 33 | #include "llvm/Transforms/Utils/Cloning.h" |
| 34 | #include "llvm/Transforms/Utils/Local.h" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | using namespace llvm::PatternMatch; |
| 38 | |
| 39 | #define DEBUG_TYPE "winehstate" |
| 40 | |
| 41 | namespace { |
| 42 | class WinEHStatePass : public FunctionPass { |
| 43 | public: |
| 44 | static char ID; // Pass identification, replacement for typeid. |
| 45 | |
| 46 | WinEHStatePass() : FunctionPass(ID) {} |
| 47 | |
| 48 | bool runOnFunction(Function &Fn) override; |
| 49 | |
| 50 | bool doInitialization(Module &M) override; |
| 51 | |
| 52 | bool doFinalization(Module &M) override; |
| 53 | |
| 54 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 55 | |
| 56 | const char *getPassName() const override { |
| 57 | return "Windows 32-bit x86 EH state insertion"; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | void emitExceptionRegistrationRecord(Function *F); |
| 62 | |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 63 | void linkExceptionRegistration(IRBuilder<> &Builder, Value *Handler); |
| 64 | void unlinkExceptionRegistration(IRBuilder<> &Builder); |
| 65 | void addCXXStateStores(Function &F, MachineModuleInfo &MMI); |
| 66 | void addCXXStateStoresToFunclet(Value *ParentRegNode, WinEHFuncInfo &FuncInfo, |
| 67 | Function &F, int BaseState); |
| 68 | void insertStateNumberStore(Value *ParentRegNode, Instruction *IP, int State); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 69 | |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 70 | Value *emitEHLSDA(IRBuilder<> &Builder, Function *F); |
| 71 | |
| 72 | Function *generateLSDAInEAXThunk(Function *ParentFunc); |
| 73 | |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 74 | int escapeRegNode(Function &F); |
| 75 | |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 76 | // Module-level type getters. |
| 77 | Type *getEHRegistrationType(); |
| 78 | Type *getSEH3RegistrationType(); |
| 79 | Type *getSEH4RegistrationType(); |
| 80 | Type *getCXXEH3RegistrationType(); |
| 81 | |
| 82 | // Per-module data. |
| 83 | Module *TheModule = nullptr; |
| 84 | StructType *EHRegistrationTy = nullptr; |
| 85 | StructType *CXXEH3RegistrationTy = nullptr; |
| 86 | StructType *SEH3RegistrationTy = nullptr; |
| 87 | StructType *SEH4RegistrationTy = nullptr; |
| 88 | |
| 89 | // Per-function state |
| 90 | EHPersonality Personality = EHPersonality::Unknown; |
| 91 | Function *PersonalityFn = nullptr; |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 92 | |
| 93 | /// The stack allocation containing all EH data, including the link in the |
| 94 | /// fs:00 chain and the current state. |
| 95 | AllocaInst *RegNode = nullptr; |
| 96 | |
| 97 | /// Struct type of RegNode. Used for GEPing. |
| 98 | Type *RegNodeTy = nullptr; |
| 99 | |
| 100 | /// The index of the state field of RegNode. |
| 101 | int StateFieldIndex = ~0U; |
| 102 | |
| 103 | /// The linked list node subobject inside of RegNode. |
| 104 | Value *Link = nullptr; |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 105 | }; |
| 106 | } |
| 107 | |
| 108 | FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); } |
| 109 | |
| 110 | char WinEHStatePass::ID = 0; |
| 111 | |
| 112 | bool WinEHStatePass::doInitialization(Module &M) { |
| 113 | TheModule = &M; |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool WinEHStatePass::doFinalization(Module &M) { |
| 118 | assert(TheModule == &M); |
| 119 | TheModule = nullptr; |
| 120 | EHRegistrationTy = nullptr; |
| 121 | CXXEH3RegistrationTy = nullptr; |
| 122 | SEH3RegistrationTy = nullptr; |
| 123 | SEH4RegistrationTy = nullptr; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 128 | // This pass should only insert a stack allocation, memory accesses, and |
| 129 | // framerecovers. |
| 130 | AU.setPreservesCFG(); |
| 131 | } |
| 132 | |
| 133 | bool WinEHStatePass::runOnFunction(Function &F) { |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 134 | // If this is an outlined handler, don't do anything. We'll do state insertion |
| 135 | // for it in the parent. |
| 136 | StringRef WinEHParentName = |
| 137 | F.getFnAttribute("wineh-parent").getValueAsString(); |
| 138 | if (WinEHParentName != F.getName() && !WinEHParentName.empty()) |
| 139 | return false; |
| 140 | |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 141 | // Check the personality. Do nothing if this is not an MSVC personality. |
| 142 | LandingPadInst *LP = nullptr; |
| 143 | for (BasicBlock &BB : F) { |
| 144 | LP = BB.getLandingPadInst(); |
| 145 | if (LP) |
| 146 | break; |
| 147 | } |
| 148 | if (!LP) |
| 149 | return false; |
| 150 | PersonalityFn = |
| 151 | dyn_cast<Function>(LP->getPersonalityFn()->stripPointerCasts()); |
| 152 | if (!PersonalityFn) |
| 153 | return false; |
| 154 | Personality = classifyEHPersonality(PersonalityFn); |
| 155 | if (!isMSVCEHPersonality(Personality)) |
| 156 | return false; |
| 157 | |
Reid Kleckner | 173a725 | 2015-05-29 21:58:11 +0000 | [diff] [blame^] | 158 | // Disable frame pointer elimination in this function. |
| 159 | // FIXME: Do the nested handlers need to keep the parent ebp in ebp, or can we |
| 160 | // use an arbitrary register? |
| 161 | F.addFnAttr("no-frame-pointer-elim", "true"); |
| 162 | |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 163 | emitExceptionRegistrationRecord(&F); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 164 | |
| 165 | auto *MMIPtr = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 166 | assert(MMIPtr && "MachineModuleInfo should always be available"); |
| 167 | MachineModuleInfo &MMI = *MMIPtr; |
| 168 | if (Personality == EHPersonality::MSVC_CXX) { |
| 169 | addCXXStateStores(F, MMI); |
| 170 | } |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 171 | |
| 172 | // Reset per-function state. |
| 173 | PersonalityFn = nullptr; |
| 174 | Personality = EHPersonality::Unknown; |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | /// Get the common EH registration subobject: |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 179 | /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)( |
| 180 | /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 181 | /// struct EHRegistrationNode { |
| 182 | /// EHRegistrationNode *Next; |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 183 | /// PEXCEPTION_ROUTINE Handler; |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 184 | /// }; |
| 185 | Type *WinEHStatePass::getEHRegistrationType() { |
| 186 | if (EHRegistrationTy) |
| 187 | return EHRegistrationTy; |
| 188 | LLVMContext &Context = TheModule->getContext(); |
| 189 | EHRegistrationTy = StructType::create(Context, "EHRegistrationNode"); |
| 190 | Type *FieldTys[] = { |
| 191 | EHRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next |
| 192 | Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...) |
| 193 | }; |
| 194 | EHRegistrationTy->setBody(FieldTys, false); |
| 195 | return EHRegistrationTy; |
| 196 | } |
| 197 | |
| 198 | /// The __CxxFrameHandler3 registration node: |
| 199 | /// struct CXXExceptionRegistration { |
| 200 | /// void *SavedESP; |
| 201 | /// EHRegistrationNode SubRecord; |
| 202 | /// int32_t TryLevel; |
| 203 | /// }; |
| 204 | Type *WinEHStatePass::getCXXEH3RegistrationType() { |
| 205 | if (CXXEH3RegistrationTy) |
| 206 | return CXXEH3RegistrationTy; |
| 207 | LLVMContext &Context = TheModule->getContext(); |
| 208 | Type *FieldTys[] = { |
| 209 | Type::getInt8PtrTy(Context), // void *SavedESP |
| 210 | getEHRegistrationType(), // EHRegistrationNode SubRecord |
| 211 | Type::getInt32Ty(Context) // int32_t TryLevel |
| 212 | }; |
| 213 | CXXEH3RegistrationTy = |
| 214 | StructType::create(FieldTys, "CXXExceptionRegistration"); |
| 215 | return CXXEH3RegistrationTy; |
| 216 | } |
| 217 | |
| 218 | /// The _except_handler3 registration node: |
| 219 | /// struct EH3ExceptionRegistration { |
| 220 | /// EHRegistrationNode SubRecord; |
| 221 | /// void *ScopeTable; |
| 222 | /// int32_t TryLevel; |
| 223 | /// }; |
| 224 | Type *WinEHStatePass::getSEH3RegistrationType() { |
| 225 | if (SEH3RegistrationTy) |
| 226 | return SEH3RegistrationTy; |
| 227 | LLVMContext &Context = TheModule->getContext(); |
| 228 | Type *FieldTys[] = { |
| 229 | getEHRegistrationType(), // EHRegistrationNode SubRecord |
| 230 | Type::getInt8PtrTy(Context), // void *ScopeTable |
| 231 | Type::getInt32Ty(Context) // int32_t TryLevel |
| 232 | }; |
| 233 | SEH3RegistrationTy = StructType::create(FieldTys, "EH3ExceptionRegistration"); |
| 234 | return SEH3RegistrationTy; |
| 235 | } |
| 236 | |
| 237 | /// The _except_handler4 registration node: |
| 238 | /// struct EH4ExceptionRegistration { |
| 239 | /// void *SavedESP; |
| 240 | /// _EXCEPTION_POINTERS *ExceptionPointers; |
| 241 | /// EHRegistrationNode SubRecord; |
| 242 | /// int32_t EncodedScopeTable; |
| 243 | /// int32_t TryLevel; |
| 244 | /// }; |
| 245 | Type *WinEHStatePass::getSEH4RegistrationType() { |
| 246 | if (SEH4RegistrationTy) |
| 247 | return SEH4RegistrationTy; |
| 248 | LLVMContext &Context = TheModule->getContext(); |
| 249 | Type *FieldTys[] = { |
| 250 | Type::getInt8PtrTy(Context), // void *SavedESP |
| 251 | Type::getInt8PtrTy(Context), // void *ExceptionPointers |
| 252 | getEHRegistrationType(), // EHRegistrationNode SubRecord |
| 253 | Type::getInt32Ty(Context), // int32_t EncodedScopeTable |
| 254 | Type::getInt32Ty(Context) // int32_t TryLevel |
| 255 | }; |
| 256 | SEH4RegistrationTy = StructType::create(FieldTys, "EH4ExceptionRegistration"); |
| 257 | return SEH4RegistrationTy; |
| 258 | } |
| 259 | |
| 260 | // Emit an exception registration record. These are stack allocations with the |
| 261 | // common subobject of two pointers: the previous registration record (the old |
| 262 | // fs:00) and the personality function for the current frame. The data before |
| 263 | // and after that is personality function specific. |
| 264 | void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) { |
| 265 | assert(Personality == EHPersonality::MSVC_CXX || |
| 266 | Personality == EHPersonality::MSVC_X86SEH); |
| 267 | |
| 268 | StringRef PersonalityName = PersonalityFn->getName(); |
| 269 | IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin()); |
| 270 | Type *Int8PtrType = Builder.getInt8PtrTy(); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 271 | if (PersonalityName == "__CxxFrameHandler3") { |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 272 | RegNodeTy = getCXXEH3RegistrationType(); |
| 273 | RegNode = Builder.CreateAlloca(RegNodeTy); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 274 | // FIXME: We can skip this in -GS- mode, when we figure that out. |
| 275 | // SavedESP = llvm.stacksave() |
| 276 | Value *SP = Builder.CreateCall( |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 277 | Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {}); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 278 | Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0)); |
| 279 | // TryLevel = -1 |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 280 | StateFieldIndex = 2; |
| 281 | insertStateNumberStore(RegNode, Builder.GetInsertPoint(), -1); |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 282 | // Handler = __ehhandler$F |
| 283 | Function *Trampoline = generateLSDAInEAXThunk(F); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 284 | Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 1); |
| 285 | linkExceptionRegistration(Builder, Trampoline); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 286 | } else if (PersonalityName == "_except_handler3") { |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 287 | RegNodeTy = getSEH3RegistrationType(); |
| 288 | RegNode = Builder.CreateAlloca(RegNodeTy); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 289 | // TryLevel = -1 |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 290 | StateFieldIndex = 2; |
| 291 | insertStateNumberStore(RegNode, Builder.GetInsertPoint(), -1); |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 292 | // ScopeTable = llvm.x86.seh.lsda(F) |
| 293 | Value *LSDA = emitEHLSDA(Builder, F); |
| 294 | Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 1)); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 295 | Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 0); |
| 296 | linkExceptionRegistration(Builder, PersonalityFn); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 297 | } else if (PersonalityName == "_except_handler4") { |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 298 | RegNodeTy = getSEH4RegistrationType(); |
| 299 | RegNode = Builder.CreateAlloca(RegNodeTy); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 300 | // SavedESP = llvm.stacksave() |
| 301 | Value *SP = Builder.CreateCall( |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 302 | Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {}); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 303 | Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0)); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 304 | // TryLevel = -1 |
| 305 | StateFieldIndex = 4; |
| 306 | insertStateNumberStore(RegNode, Builder.GetInsertPoint(), -1); |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 307 | // FIXME: XOR the LSDA with __security_cookie. |
| 308 | // ScopeTable = llvm.x86.seh.lsda(F) |
| 309 | Value *FI8 = Builder.CreateBitCast(F, Int8PtrType); |
| 310 | Value *LSDA = Builder.CreateCall( |
| 311 | Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8); |
| 312 | Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 1)); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 313 | Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2); |
| 314 | linkExceptionRegistration(Builder, PersonalityFn); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 315 | } else { |
| 316 | llvm_unreachable("unexpected personality function"); |
| 317 | } |
| 318 | |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 319 | // Insert an unlink before all returns. |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 320 | for (BasicBlock &BB : *F) { |
| 321 | TerminatorInst *T = BB.getTerminator(); |
| 322 | if (!isa<ReturnInst>(T)) |
| 323 | continue; |
| 324 | Builder.SetInsertPoint(T); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 325 | unlinkExceptionRegistration(Builder); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 329 | Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) { |
| 330 | Value *FI8 = Builder.CreateBitCast(F, Type::getInt8PtrTy(F->getContext())); |
| 331 | return Builder.CreateCall( |
| 332 | Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8); |
| 333 | } |
| 334 | |
| 335 | /// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls |
| 336 | /// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE: |
| 337 | /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)( |
| 338 | /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *); |
| 339 | /// We essentially want this code: |
| 340 | /// movl $lsda, %eax |
| 341 | /// jmpl ___CxxFrameHandler3 |
| 342 | Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) { |
| 343 | LLVMContext &Context = ParentFunc->getContext(); |
| 344 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 345 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
| 346 | Type *ArgTys[5] = {Int8PtrType, Int8PtrType, Int8PtrType, Int8PtrType, |
| 347 | Int8PtrType}; |
| 348 | FunctionType *TrampolineTy = |
| 349 | FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 4), |
| 350 | /*isVarArg=*/false); |
| 351 | FunctionType *TargetFuncTy = |
| 352 | FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 5), |
| 353 | /*isVarArg=*/false); |
| 354 | Function *Trampoline = Function::Create( |
| 355 | TrampolineTy, GlobalValue::InternalLinkage, |
| 356 | Twine("__ehhandler$") + ParentFunc->getName(), TheModule); |
| 357 | BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline); |
| 358 | IRBuilder<> Builder(EntryBB); |
| 359 | Value *LSDA = emitEHLSDA(Builder, ParentFunc); |
| 360 | Value *CastPersonality = |
| 361 | Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo()); |
| 362 | auto AI = Trampoline->arg_begin(); |
| 363 | Value *Args[5] = {LSDA, AI++, AI++, AI++, AI++}; |
| 364 | CallInst *Call = Builder.CreateCall(CastPersonality, Args); |
| 365 | // Can't use musttail due to prototype mismatch, but we can use tail. |
| 366 | Call->setTailCall(true); |
| 367 | // Set inreg so we pass it in EAX. |
| 368 | Call->addAttribute(1, Attribute::InReg); |
| 369 | Builder.CreateRet(Call); |
| 370 | return Trampoline; |
| 371 | } |
| 372 | |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 373 | void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder, |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 374 | Value *Handler) { |
| 375 | Type *LinkTy = getEHRegistrationType(); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 376 | // Handler = Handler |
| 377 | Handler = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy()); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 378 | Builder.CreateStore(Handler, Builder.CreateStructGEP(LinkTy, Link, 1)); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 379 | // Next = [fs:00] |
| 380 | Constant *FSZero = |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 381 | Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 382 | Value *Next = Builder.CreateLoad(FSZero); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 383 | Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0)); |
| 384 | // [fs:00] = Link |
| 385 | Builder.CreateStore(Link, FSZero); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 388 | void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) { |
| 389 | // Clone Link into the current BB for better address mode folding. |
| 390 | if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) { |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 391 | GEP = cast<GetElementPtrInst>(GEP->clone()); |
| 392 | Builder.Insert(GEP); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 393 | Link = GEP; |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 394 | } |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 395 | Type *LinkTy = getEHRegistrationType(); |
| 396 | // [fs:00] = Link->Next |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 397 | Value *Next = |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 398 | Builder.CreateLoad(Builder.CreateStructGEP(LinkTy, Link, 0)); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 399 | Constant *FSZero = |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 400 | Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 401 | Builder.CreateStore(Next, FSZero); |
| 402 | } |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 403 | |
| 404 | void WinEHStatePass::addCXXStateStores(Function &F, MachineModuleInfo &MMI) { |
| 405 | WinEHFuncInfo &FuncInfo = MMI.getWinEHFuncInfo(&F); |
| 406 | calculateWinCXXEHStateNumbers(&F, FuncInfo); |
| 407 | |
| 408 | // The base state for the parent is -1. |
| 409 | addCXXStateStoresToFunclet(RegNode, FuncInfo, F, -1); |
| 410 | |
| 411 | // Set up RegNodeEscapeIndex |
| 412 | int RegNodeEscapeIndex = escapeRegNode(F); |
| 413 | |
| 414 | // Only insert stores in catch handlers. |
| 415 | Function *FrameRecover = |
| 416 | Intrinsic::getDeclaration(TheModule, Intrinsic::framerecover); |
| 417 | Function *FrameAddress = |
| 418 | Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress); |
| 419 | Constant *FI8 = |
| 420 | ConstantExpr::getBitCast(&F, Type::getInt8PtrTy(TheModule->getContext())); |
| 421 | for (auto P : FuncInfo.HandlerBaseState) { |
| 422 | Function *Handler = const_cast<Function *>(P.first); |
| 423 | int BaseState = P.second; |
| 424 | IRBuilder<> Builder(&Handler->getEntryBlock(), |
| 425 | Handler->getEntryBlock().begin()); |
| 426 | // FIXME: Find and reuse such a call if present. |
| 427 | Value *ParentFP = Builder.CreateCall(FrameAddress, {Builder.getInt32(1)}); |
| 428 | Value *RecoveredRegNode = Builder.CreateCall( |
| 429 | FrameRecover, {FI8, ParentFP, Builder.getInt32(RegNodeEscapeIndex)}); |
| 430 | RecoveredRegNode = |
| 431 | Builder.CreateBitCast(RecoveredRegNode, RegNodeTy->getPointerTo(0)); |
| 432 | addCXXStateStoresToFunclet(RecoveredRegNode, FuncInfo, *Handler, BaseState); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /// Escape RegNode so that we can access it from child handlers. Find the call |
| 437 | /// to frameescape, if any, in the entry block and append RegNode to the list |
| 438 | /// of arguments. |
| 439 | int WinEHStatePass::escapeRegNode(Function &F) { |
| 440 | // Find the call to frameescape and extract its arguments. |
| 441 | IntrinsicInst *EscapeCall = nullptr; |
| 442 | for (Instruction &I : F.getEntryBlock()) { |
| 443 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); |
| 444 | if (II && II->getIntrinsicID() == Intrinsic::frameescape) { |
| 445 | EscapeCall = II; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | SmallVector<Value *, 8> Args; |
| 450 | if (EscapeCall) { |
| 451 | auto Ops = EscapeCall->arg_operands(); |
| 452 | Args.append(Ops.begin(), Ops.end()); |
| 453 | } |
| 454 | Args.push_back(RegNode); |
| 455 | |
| 456 | // Replace the call (if it exists) with new one. Otherwise, insert at the end |
| 457 | // of the entry block. |
| 458 | IRBuilder<> Builder(&F.getEntryBlock(), |
| 459 | EscapeCall ? EscapeCall : F.getEntryBlock().end()); |
| 460 | Builder.CreateCall( |
| 461 | Intrinsic::getDeclaration(TheModule, Intrinsic::frameescape), Args); |
| 462 | if (EscapeCall) |
| 463 | EscapeCall->eraseFromParent(); |
| 464 | return Args.size() - 1; |
| 465 | } |
| 466 | |
| 467 | void WinEHStatePass::addCXXStateStoresToFunclet(Value *ParentRegNode, |
| 468 | WinEHFuncInfo &FuncInfo, |
| 469 | Function &F, int BaseState) { |
| 470 | // Iterate all the instructions and emit state number stores. |
| 471 | for (BasicBlock &BB : F) { |
| 472 | for (Instruction &I : BB) { |
| 473 | if (auto *CI = dyn_cast<CallInst>(&I)) { |
| 474 | // Possibly throwing call instructions have no actions to take after |
| 475 | // an unwind. Ensure they are in the -1 state. |
| 476 | if (CI->doesNotThrow()) |
| 477 | continue; |
| 478 | insertStateNumberStore(ParentRegNode, CI, BaseState); |
| 479 | } else if (auto *II = dyn_cast<InvokeInst>(&I)) { |
| 480 | // Look up the state number of the landingpad this unwinds to. |
| 481 | LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst(); |
| 482 | // FIXME: Why does this assertion fail? |
| 483 | //assert(FuncInfo.LandingPadStateMap.count(LPI) && "LP has no state!"); |
| 484 | int State = FuncInfo.LandingPadStateMap[LPI]; |
| 485 | insertStateNumberStore(ParentRegNode, II, State); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void WinEHStatePass::insertStateNumberStore(Value *ParentRegNode, |
| 492 | Instruction *IP, int State) { |
| 493 | IRBuilder<> Builder(IP); |
| 494 | Value *StateField = |
| 495 | Builder.CreateStructGEP(RegNodeTy, ParentRegNode, StateFieldIndex); |
| 496 | Builder.CreateStore(Builder.getInt32(State), StateField); |
| 497 | } |