[llvm] r239451 - [WinEH] Call llvm.stackrestore in __except blocks

Reid Kleckner reid at kleckner.net
Tue Jun 9 18:34:54 PDT 2015


Author: rnk
Date: Tue Jun  9 20:34:54 2015
New Revision: 239451

URL: http://llvm.org/viewvc/llvm-project?rev=239451&view=rev
Log:
[WinEH] Call llvm.stackrestore in __except blocks

We have to do this manually, the runtime only sets up ebp. Fixes a crash
when returning after catching an exception.

Modified:
    llvm/trunk/lib/Target/X86/X86WinEHState.cpp
    llvm/trunk/test/CodeGen/X86/seh-safe-div-win32.ll

Modified: llvm/trunk/lib/Target/X86/X86WinEHState.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86WinEHState.cpp?rev=239451&r1=239450&r2=239451&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86WinEHState.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86WinEHState.cpp Tue Jun  9 20:34:54 2015
@@ -263,7 +263,6 @@ void WinEHStatePass::emitExceptionRegist
   if (Personality == EHPersonality::MSVC_CXX) {
     RegNodeTy = getCXXEHRegistrationType();
     RegNode = Builder.CreateAlloca(RegNodeTy);
-    // FIXME: We can skip this in -GS- mode, when we figure that out.
     // SavedESP = llvm.stacksave()
     Value *SP = Builder.CreateCall(
         Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
@@ -490,6 +489,7 @@ void WinEHStatePass::addSEHStateStores(F
 
   // Iterate all the instructions and emit state number stores.
   int CurState = 0;
+  SmallPtrSet<BasicBlock *, 4> ExceptBlocks;
   for (BasicBlock &BB : F) {
     for (auto I = BB.begin(), E = BB.end(); I != E; ++I) {
       if (auto *CI = dyn_cast<CallInst>(I)) {
@@ -517,11 +517,29 @@ void WinEHStatePass::addSEHStateStores(F
           assert(!ActionList.empty());
           CurState += ActionList.size();
           State += ActionList.size() - 1;
+
+          // Remember all the __except block targets.
+          for (auto &Handler : ActionList) {
+            if (auto *CH = dyn_cast<CatchHandler>(Handler.get())) {
+              auto *BA = cast<BlockAddress>(CH->getHandlerBlockOrFunc());
+              ExceptBlocks.insert(BA->getBasicBlock());
+            }
+          }
         }
         insertStateNumberStore(RegNode, II, State);
       }
     }
   }
+
+  // Insert llvm.stackrestore into each __except block.
+  Function *StackRestore =
+      Intrinsic::getDeclaration(TheModule, Intrinsic::stackrestore);
+  for (BasicBlock *ExceptBB : ExceptBlocks) {
+    IRBuilder<> Builder(ExceptBB->begin());
+    Value *SP =
+        Builder.CreateLoad(Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
+    Builder.CreateCall(StackRestore, {SP});
+  }
 }
 
 /// Rewrite llvm.eh.exceptioncode and llvm.eh.exceptioninfo to memory loads in

Modified: llvm/trunk/test/CodeGen/X86/seh-safe-div-win32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/seh-safe-div-win32.ll?rev=239451&r1=239450&r2=239451&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/seh-safe-div-win32.ll (original)
+++ llvm/trunk/test/CodeGen/X86/seh-safe-div-win32.ll Tue Jun  9 20:34:54 2015
@@ -77,11 +77,15 @@ __try.cont:
 
 ; CHECK: [[handler0:Ltmp[0-9]+]]: # Block address taken
 ; CHECK: # %handler0
+; 	Restore SP
+; CHECK: movl {{.*}}(%ebp), %esp
 ; CHECK: calll _puts
 ; CHECK: jmp [[cont_bb]]
 
 ; CHECK: [[handler1:Ltmp[0-9]+]]: # Block address taken
 ; CHECK: # %handler1
+; 	Restore SP
+; CHECK: movl {{.*}}(%ebp), %esp
 ; CHECK: calll _puts
 ; CHECK: jmp [[cont_bb]]
 





More information about the llvm-commits mailing list